예제 #1
0
        /// <summary>
        /// 获取所有分中心信息
        /// </summary>
        public List <CEntitySerialPort> QueryAll()
        {
            var result  = new List <CEntitySerialPort>();
            var sqlConn = CDBManager.GetInstacne().GetConnection();

            try
            {
                m_mutexWriteToDB.WaitOne();         // 取对数据库的唯一访问权
                m_mutexDataTable.WaitOne();         // 获取内存表的访问权
                sqlConn.Open();                     // 建立数据库连接

                String sqlStr = GetQuerySQL();

                SqlCommand sqlCmd = new SqlCommand(sqlStr, sqlConn);

                SqlDataReader reader = sqlCmd.ExecuteReader();

                Debug.Assert(reader.FieldCount == CN_FIELD_COUNT, CT_TableName + "表与类" + CT_EntityName + "中定义字段不符合");

                //  处理查询结果
                while (reader.Read())
                {
                    try
                    {
                        var port = new CEntitySerialPort();

                        port.PortNumber  = (Int32)reader[CN_PortNumber];
                        port.TransType   = CEnumHelper.DBStrToSerialTransType((String)reader[CN_TransType]);
                        port.Baudrate    = (Int32)reader[CN_Baudrate];
                        port.DataBit     = (Int32)reader[CN_Databit];
                        port.StopBit     = (Int32)reader[CN_Stopbit];
                        port.ParityType  = CEnumHelper.DBCharToPortParityType(((String)reader[CN_Parity])[0]);
                        port.Stream      = CEnumHelper.DBStrToSerialPortStreamType(reader[CN_Stream].ToString());
                        port.Break       = (Boolean)reader[CN_Break];
                        port.SwitchSatus = (Boolean)reader[CN_Open];
                        result.Add(port);
                    }
                    catch (Exception exp)
                    {
                        Debug.WriteLine(exp.Message);
                    }
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                sqlConn.Close();                    //  关闭数据库连接
                m_mutexDataTable.ReleaseMutex();    //  释放内存表的访问权
                m_mutexWriteToDB.ReleaseMutex();    //  释放数据库的访问权
            }
            return(result);
        }