// 添加新列 public void AddNewRow(CEntitySerialPort entity) { // 记录超过1000条,或者时间超过1分钟,就将当前的数据写入数据库 m_mutexDataTable.WaitOne(); //等待互斥量 DataRow row = m_tableDataAdded.NewRow(); row[CN_PortNumber] = entity.PortNumber; row[CN_TransType] = CEnumHelper.SerialTransTypeToDBStr(entity.TransType); row[CN_Baudrate] = entity.Baudrate; row[CN_Databit] = entity.DataBit; row[CN_Stopbit] = entity.StopBit; row[CN_Parity] = CEnumHelper.PortParityTypeToDBChar(entity.ParityType); row[CN_Stream] = CEnumHelper.SerialPortStreamTypeToDBStr(entity.Stream); row[CN_Break] = entity.Break; row[CN_Open] = entity.SwitchSatus; m_tableDataAdded.Rows.Add(row); if (m_tableDataAdded.Rows.Count >= CDBParams.GetInstance().AddBufferMax) { // 如果超过最大值,写入数据库 Task task = new Task(() => { AddDataToDB(); }); task.Start(); } else { // 没有超过缓存最大值,开启定时器进行检测,多次调用Start()会导致重新计数 m_addTimer.Start(); } m_mutexDataTable.ReleaseMutex(); }
/// <summary> /// 更新串口信息 /// </summary> /// <param name="listUser"></param> /// <returns></returns> public bool UpdateRange(List <CEntitySerialPort> listPort) { StringBuilder sql = new StringBuilder(); foreach (var item in listPort) { string transType = CEnumHelper.SerialTransTypeToDBStr(item.TransType); char parity = CEnumHelper.PortParityTypeToDBChar(item.ParityType); string breakValue = item.Break.HasValue ? item.Break.Value.ToString() : "null"; string open = item.SwitchSatus.HasValue ? item.SwitchSatus.Value.ToString() : "null"; sql.AppendFormat("UPDATE {0} SET [{1}]='{2}',[{3}]='{4}',[{5}]='{6}',[{7}]='{8}',[{9}]='{10}',[{11}]='{12}',[{13}]='{14}',[{15}]='{16}' WHERE [{17}]={18}\r", CT_TableName, CN_TransType, transType, CN_Baudrate, item.Baudrate, CN_Databit, item.DataBit, CN_Stopbit, item.StopBit, CN_Parity, parity, CN_Stream, CEnumHelper.SerialPortStreamTypeToDBStr(item.Stream), CN_Break, breakValue, CN_Open, open, CN_PortNumber, item.PortNumber ); } return(base.ExecuteSQLCommand(sql.ToString())); }
private String GetUpdateSQL(CEntitySerialPort entity) { String strPortNum = entity.PortNumber.ToString(); String strTransType = CEnumHelper.SerialTransTypeToDBStr(entity.TransType); String strBaudrate = entity.Baudrate.ToString(); String strDataBit = entity.DataBit.ToString(); String strStopBit = entity.StopBit.ToString(); String strParityType = CEnumHelper.PortParityTypeToDBChar(entity.ParityType).ToString(); String strStream = CEnumHelper.SerialPortStreamTypeToDBStr(entity.Stream); String strBreak = "NULL"; if (entity.Break.HasValue) { strBreak = entity.Break.Value.ToString(); //if (entity.Break.Value) // strBreak = "1"; //else // strBreak = "0"; } String strOpen = "NULL"; if (entity.SwitchSatus.HasValue) { strOpen = entity.SwitchSatus.Value.ToString(); //if (entity.SwitchSatus.Value) // strOpen = "1"; //else // strOpen = "0"; } return(String.Format( "UPDATE {0} SET [{1}]={2},[{3}]={4},[{5}]={6},[{7}]={8},[{9}]={10},[{11}]={12},[{13}]={14},[{15}]={16} WHERE [{17}]={18}", CT_TableName, CN_TransType, strTransType, CN_Baudrate, strBaudrate, CN_Databit, strDataBit, CN_Stopbit, strStopBit, CN_Parity, strParityType, CN_Stream, strStream, CN_Break, strBreak, CN_Open, strOpen, CN_PortNumber, strPortNum )); }
private String GetInsertSQL(CEntitySerialPort entity) { String strPortNum = entity.PortNumber.ToString(); String strTransType = CEnumHelper.SerialTransTypeToDBStr(entity.TransType); String strBaudrate = entity.Baudrate.ToString(); String strDataBit = entity.DataBit.ToString(); String strStopBit = entity.StopBit.ToString(); String strParityType = CEnumHelper.PortParityTypeToDBChar(entity.ParityType).ToString(); String strStream = CEnumHelper.SerialPortStreamTypeToDBStr(entity.Stream); String strBreak = "NULL"; if (entity.Break.HasValue) { strBreak = entity.Break.Value.ToString(); //if (entity.Break.Value) // strBreak = "1"; //else // strBreak = "0"; } String strOpen = "NULL"; if (entity.SwitchSatus.HasValue) { strBreak = entity.SwitchSatus.Value.ToString(); //if (entity.SwitchSatus.Value) // strOpen = "1"; //else // strOpen = "0"; } return(String.Format( "INSERT INTO {0} ([{1}],[{2}],[{3}],[{4}],[{5}],[{6}],[{7}],[{8}],[{9}]) VALUES ({10},{11},{12},{13},{14},{15},{16},{17},{18})", CT_TableName, CN_PortNumber, CN_TransType, CN_Baudrate, CN_Databit, CN_Stopbit, CN_Parity, CN_Stream, CN_Break, CN_Open, strPortNum, strTransType, strBaudrate, strDataBit, strStopBit, strParityType, strStream, strBreak, strOpen )); }