private static void InsertSql(object sender, System.Timers.ElapsedEventArgs e) { SqlInsertTimer.Enabled = false; SQLClient sqlclnt = new SQLClient(sLogFilePath); DataTable dtItems = new DataTable("OPCItems"); dtItems.Columns.AddRange(new DataColumn[] { new DataColumn("ItemID"), new DataColumn("ItemValue"), new DataColumn("ItemQualitie"), new DataColumn("ItemTimeStamp"), new DataColumn("UpdateTime"), }); sqlclnt.sConnectStr = sSqlStr; bool bsqlcnt = sqlclnt.ConnectSql(); if (bsqlcnt == true) { DateTime UpdataTime = DateTime.Now; for (int i = 0; i < opcKepClient.lKepItem.Count; i++) { string ItemID = opcKepClient.lKepItem[i].ItemID; var ItemValue = opcKepClient.lKepItem[i].Value; int? ItemQuality = opcKepClient.lKepItem[i].Quality; DateTime?ItemTimeStamp = opcKepClient.lKepItem[i].TimeStamp.AddHours(8); DataRow drItem = dtItems.NewRow(); drItem[0] = opcKepClient.lKepItem[i].ItemID; drItem[1] = opcKepClient.lKepItem[i].Value; drItem[2] = opcKepClient.lKepItem[i].Quality; drItem[3] = opcKepClient.lKepItem[i].TimeStamp.AddHours(8); drItem[4] = DateTime.Now; dtItems.Rows.Add(drItem); } } int iUpdateRows; string sqldelet = string.Format("delete from {0} where 1=1 ", "Runtimes"); sqlclnt.sqlExecuteNonQuery(sqldelet, out iUpdateRows, CommandType.Text); sqlclnt.sqlBulkCopyData("Runtimes", dtItems); sqlclnt.SqlClose(); SqlInsertTimer.Enabled = true; }
private static void InsertSql(object sender, System.Timers.ElapsedEventArgs e) { SqlInsertTimer.Enabled = false; SQLClient sqlclnt = new SQLClient(); DataTable dtItems = new DataTable("OPCItems"); dtItems.Columns.AddRange(new DataColumn[] { /*new DataColumn("ItemID",typeof(string)), * new DataColumn("ItemValue",typeof(object)), * new DataColumn("ItemQualitie",typeof(int)), * new DataColumn("ItemTimeStamp",typeof(DateTime)), * new DataColumn("UpdateTime",typeof(DateTime)),*/ new DataColumn("ItemID"), new DataColumn("ItemValue"), new DataColumn("ItemQualitie"), new DataColumn("ItemTimeStamp"), new DataColumn("UpdateTime"), }); sqlclnt.sConnectStr = sSqlStr; bool bsqlcnt = sqlclnt.ConnectSql(); if (bsqlcnt == true) { DateTime UpdataTime = DateTime.Now; for (int i = 0; i < opcKepClient.lKepItem.Count; i++) { //int Index =(int) ClientHandles.GetValue(i); string ItemID = opcKepClient.lKepItem[i].ItemID; var ItemValue = opcKepClient.lKepItem[i].Value; int? ItemQuality = opcKepClient.lKepItem[i].Quality; DateTime?ItemTimeStamp = opcKepClient.lKepItem[i].TimeStamp.AddHours(8); DataRow drItem = dtItems.NewRow(); drItem[0] = opcKepClient.lKepItem[i].ItemID; drItem[1] = opcKepClient.lKepItem[i].Value; drItem[2] = opcKepClient.lKepItem[i].Quality; drItem[3] = opcKepClient.lKepItem[i].TimeStamp.AddHours(8); drItem[4] = DateTime.Now; dtItems.Rows.Add(drItem); /*string sqldelet = string.Format("delete from {0} where {1}='{2}' ", "Runtimes", "ItemID", ItemID); * string sqlInsert = string.Format("INSERT INTO {0} VALUES ('{1}','{2}','{3}','{4}','{5}') ", "Runtimes", * ItemID, ItemValue, ItemQuality, ItemTimeStamp, UpdataTime); * * sqlclnt.sqlExecuteNonQuery(sqldelet); * sqlclnt.sqlExecuteNonQuery(sqlInsert);*/ /*string sqlUpdata = string.Format("update {0} set {3}='{4}',{5}='{6}',{7}='{8}',{9}='{10}' where {1}='{2}'", "Runtimes", "ItemID", ItemID, * "ItemValue", ItemValue, "ItemQualitie", ItemQuality, "ItemTimeStamp", ItemTimeStamp, "UpdateTime", DateTime.Now); * int iUpdateRows; * sqlclnt.sqlExecuteNonQuery(sqlUpdata,out iUpdateRows); * if (iUpdateRows == 0) * { * string sqlInsert = string.Format("INSERT INTO {0} VALUES ('{1}','{2}','{3}','{4}','{5}') ", "Runtimes", * ItemID, ItemValue, ItemQuality, ItemTimeStamp, DateTime.Now); * sqlclnt.sqlExecuteNonQuery(sqlInsert, out iUpdateRows); * }*/ } } int iUpdateRows; string sqldelet = string.Format("delete from {0} where 1=1 ", "Runtimes"); sqlclnt.sqlExecuteNonQuery(sqldelet, out iUpdateRows, CommandType.Text); sqlclnt.sqlBulkCopyData("Runtimes", dtItems); sqlclnt.SqlClose(); SqlInsertTimer.Enabled = true; }