Esempio n. 1
0
        public DataTable GetDeviceList(bool getAll = true)
        {
            string sql = string.Empty;

            sql += " SELECT ";
            sql += "    * ";
            sql += " FROM ";
            sql += "    m_device ";
            sql += " WHERE ";
            sql += "    {0} " + (!getAll ? " AND device_type > 0" : "");
            sql += " ORDER BY ";
            sql += "    device_type ASC ";

            DataTable rst           = null;
            DataTable defaultDevice = null;
            Dictionary <string, object> deviceData = null;

            try
            {
                rst = _db.GetTable(string.Format(sql, " userid = " + userGetDeviceList));

                if (Common.TableIsNullOrEmpty(rst) && getAll)
                {
                    defaultDevice = _db.GetTable(string.Format(sql, " userid IS NULL "));

                    if (!Common.TableIsNullOrEmpty(defaultDevice))
                    {
                        _db.BeginTransaction();

                        foreach (DataRow row in defaultDevice.Rows)
                        {
                            deviceData = new Dictionary <string, object>();
                            deviceData.Add("device_name", Common.CnvNullToString(row["device_name"]));
                            deviceData.Add("ip_address", Common.CnvNullToString(row["ip_address"]));
                            deviceData.Add("port", Common.CnvNullToInt(row["port"]));
                            deviceData.Add("alarm_value", Common.CnvNullToInt(row["alarm_value"]));
                            deviceData.Add("period", Common.CnvNullToInt(row["period"]));
                            deviceData.Add("fail_level", Common.CnvNullToInt(row["fail_level"]));
                            deviceData.Add("samples", Common.CnvNullToInt(row["samples"]));
                            deviceData.Add("active", Common.CnvNullToInt(row["active"]));
                            deviceData.Add("device_type", Common.CnvNullToInt(row["device_type"]));
                            deviceData.Add("userid", userGetDeviceList);

                            if (!_db.InsertTable("m_device", deviceData, false))
                            {
                                _db.Rollback();
                                return(rst);
                            }
                        }

                        _db.Commit();

                        rst = _db.GetTable(string.Format(sql, " userid = " + userGetDeviceList));
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                _lastSqlQuery = _db.LastSqlQuery;
                Common.TableDispose(ref defaultDevice);
                deviceData = null;
            }

            return(rst);
        }