Prechádzať zdrojové kódy

插入数据库前按抄表时间进行排序

yinyujing 4 rokov pred
rodič
commit
77099e483d

+ 29 - 6
NB_IOT_TCP_HP_SOCKET/WWKJUtil/ProtocolAnalysisTools.cs

@@ -524,6 +524,10 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
         public static void SaveRecord(List<HistoryRecord> list)
         {
             Dictionary<string, HistoryRecord> lastData = new Dictionary<string, HistoryRecord>();
+            if (list != null && list.Count > 1)
+            {
+                list.Sort(SortList);
+            }
             foreach (HistoryRecord record2 in list)
             {
                 try
@@ -542,8 +546,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     if (!CheckRecordExsit(tablename, record2.RecordTime))
                     {
                         StringBuilder strSql = new StringBuilder();
-                        strSql.Append("DECLARE @NewId int SELECT @NewId = (isnull(MAX(ID), 0) + 1) FROM ").Append(tablename);
-                        strSql.Append(" insert into ").Append(tablename).Append(" ( ID,设备ID,记录时间,采集时间,设备状态,通讯状态,数据来源");
+                        strSql.Append(" insert into ").Append(tablename).Append(" ( 设备ID,记录时间,采集时间,设备状态,通讯状态,数据来源");
                         List<CommData> dataList = record2.DataList;
                         StringBuilder cellValue = new StringBuilder();
                         for (int i = 0; i < dataList.Count; i++)
@@ -566,7 +569,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                                 }
                             }
                         }
-                        strSql.Append(") values (@NewId,").Append(deviceid).Append(",GETDATE(),'").Append(record2.RecordTime.ToString()).Append("','全部正常','全部正常','").Append(record2.DataSource).Append("'").Append(cellValue.ToString()).Append(")");
+                        strSql.Append(") values (").Append(deviceid).Append(",GETDATE(),'").Append(record2.RecordTime.ToString()).Append("','全部正常','全部正常','").Append(record2.DataSource).Append("'").Append(cellValue.ToString()).Append(")");
 
                         int rs = DBHelper.Query(strSql.ToString());
 
@@ -692,8 +695,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             // 如果不存在该设备数据则进行更新
             if (dataTable.Rows.Count == 0)
             {
-                strSql.Append("DECLARE @NewId int SELECT @NewId = (isnull(MAX(ID), 0) + 1) FROM 最后状态");
-                strSql.Append(" insert into 最后状态( ID,设备ID,记录时间,采集时间,设备状态,通讯状态,数据来源");
+                strSql.Append(" insert into 最后状态( 设备ID,记录时间,采集时间,设备状态,通讯状态,数据来源");
                 
                 foreach (CommData commData in dataList)
                 {
@@ -715,7 +717,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                         }
                     }
                 }
-                strSql.Append(") values (@NewId,").Append(deviceId).Append(",GETDATE(),'").Append(recDate).Append("','全部正常','全部正常','").Append(dataSource).Append("'").Append(valueStr.ToString()).Append(")");
+                strSql.Append(") values (").Append(deviceId).Append(",GETDATE(),'").Append(recDate).Append("','全部正常','全部正常','").Append(dataSource).Append("'").Append(valueStr.ToString()).Append(")");
                 DBHelper.Query(strSql.ToString());
             }
             else
@@ -983,5 +985,26 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
             }
             return sendInfo;
         }
+
+        /// <summary>
+        /// 对List进行排序
+        /// </summary>
+        /// <param name="a"></param>
+        /// <param name="b"></param>
+        /// <returns></returns>
+        private static int SortList(HistoryRecord a, HistoryRecord b) 
+        {
+            if (a.RecordTime > b.RecordTime)
+            {
+                return 1;
+            }
+            else if (a.RecordTime < b.RecordTime)
+
+            {
+                return -1;
+            }
+            return 0;
+
+        }
     }
 }