⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sp.cs

📁 联通的SGIP发送代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
        public int KeyID
        {
            get { return _dbKeyID; }
            set { _dbKeyID = value; }
        }

        /// <summary>
        /// 二级sp的接入号
        /// </summary>
        public string SPNumber
        {
            get { return _spNumber; }
            set 
            {
                SgipHelper.CheckMaxBytes(value, 21);
                _spNumber = value; 
            }
        }

        /// <summary>
        /// 二级sp的业务代码,对应于Submit的ServiceType字段(?)
        /// </summary>
        public string ServiceTag
        {
            get { return _serviceTag; }
            set 
            {
                SgipHelper.CheckMaxBytes(value, 10);
                _serviceTag = value; 
            }
        }

        /// <summary>
        /// 二级sp的结点编码,应将它设为一级sp的结点编号(NodeID)
        /// </summary>
        public string SPID
        {
            get { return _spID; }
            set 
            {
                SgipHelper.CheckMaxBytes(value, 10);
                _spID = value; 
            }
        }

        /// <summary>
        /// 二级sp的企业代码,应与一级sp的企业代码相同
        /// </summary>
        public string CorpID
        {
            get { return _corpID; }
            set 
            {
                SgipHelper.CheckMaxBytes(value, 5);
                _corpID = value; 
            }
        }

        /// <summary>
        /// 二级sp的IP地址
        /// </summary>
        public int IP 
        {
            get { return _ip; }
            set
            {
                _ip = value;
            }
        }

        /// <summary>
        /// 二级sp所监听的端口,当有信息传给它时,通过IP和ListenPort传过去
        /// </summary>
        public int ListenPort
        {
            get { return _listenPort; }
            set
            {
                _listenPort = value;
            }
        }

        /// <summary>
        /// 二级sp的登录名
        /// </summary>
        public string LoginName
        {
            get { return _loginName; }
            set 
            {
                SgipHelper.CheckMaxBytes(value, 16);
                _loginName = value; 
            }
        }

        /// <summary>
        /// 二级sp的密码
        /// </summary>
        public string Password
        {
            get { return _password; }
            set 
            {
                SgipHelper.CheckMaxBytes(value, 16);
                _password = value; 
            }
        }

    }

     
    public class SubSPManager
    {
        private SubSPCollection _spList = new SubSPCollection();
        private SqlConnection _con;
        private SqlCommand _cmd;

        internal SubSPManager()
        {
            _con = new SqlConnection(SgipConfig.Database.ConnectionString);
            _cmd = _con.CreateCommand();
            _cmd.CommandTimeout = 120;
            this.LoadDataFromDatabase();
        }

        private static SubSPManager _spManager = null;

        /// <summary>
        /// 必须先用BuildManager方法才能调用Manager属性!ps:static变量是在第一次用到时初始化,由于这里要预先建立起缓冲,所以就必须调用这个方法
        /// </summary>
        public static void BuildManager()
        {
            if (_spManager == null)
            {
                _spManager = new SubSPManager();
            }
        }

        public static SubSPManager Manager
        {
           get 
           {
               if (_spManager == null)
               {
                   BuildManager();
               }
               return _spManager; 
           }            
        }

        private void OnAfterFetchData(IAsyncResult ar)
        {
            try
            {
                SqlCommand cmd = (SqlCommand)ar.AsyncState;
                using (SqlDataReader dr = cmd.EndExecuteReader(ar))
                {
                    lock (this)
                    {
                        _spList.Clear();
                        while (dr.Read())
                        {
                            SubSP sp = new SubSP();
                            sp.KeyID = (int)dr["DBkeyID"];
                            sp.SPNumber = (string)dr["SPNumber"];
                            sp.ServiceTag = (string)dr["ServiceTag"];
                            sp.SPID = (string)dr["SPID"];
                            sp.CorpID = (string)dr["CorpId"];
                            sp.IP = (int)dr["SPIP"];
                            sp.ListenPort = (int)dr["SPListenPort"];
                            sp.LoginName = (string)dr["LoginName"];
                            sp.Password = (string)dr["LoginName"];
                            _spList.Add(sp);
                        }
                    }
                }
            }catch(Exception e){
                System.Diagnostics.Debug.WriteLine(e);
            }
        }

        /// <summary>
        /// 从数据库[subSP表中读取所有的二级Sp
        /// </summary>
        private void LoadDataFromDatabase()
        {
            try
            {
                _cmd.CommandText = "select * from [subSP]";
                _cmd.Connection.Open();
                _cmd.BeginExecuteReader(new AsyncCallback(OnAfterFetchData), _cmd, System.Data.CommandBehavior.CloseConnection);
            }
            catch
            {
                _cmd.Connection.Close();
            }
        }

        /// <summary>
        /// 向数据插入一条二级sp的记录,同时存入缓冲
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public bool Insert(SubSP sp)
        {
            _cmd.CommandText = string.Format(
                @"INSERT INTO subSP(SPNumber, ServiceTag, SPID, CorpId, SPIP, SPListenPort, LoginName, Password) values('{0}', '{1}', '{2}', '{3}', '{4}', {5}, {6}, '{7}'",
                sp.SPNumber, sp.ServiceTag, sp.SPID, sp.CorpID, sp.IP, sp.ListenPort, sp.LoginName, sp.Password);
            _cmd.Connection.Open();
            bool bResult = false;
            try
            {
                bResult = (_cmd.ExecuteNonQuery() == 1);
            }
            finally
            {
                _cmd.Connection.Close();
                if (bResult)
                {
                    lock (this)
                    {
                        _spList.Add(sp);
                    }
                }
            }
            return bResult;
        }

        /// <summary>
        /// 更新一个二级sp
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public bool Update(SubSP sp)
        {
            _cmd.CommandText = string.Format(
                  @"update [subSP] 
                    set SPNumber='{0}', ServiceTag = '{1}', SPID = '{2}', CorpId = '{3}', 
                        SPIP = {4}, SPListenPort = {5}, LoginName = '{6}', Password = '{7}'
                    where DBkeyID = {8}",
                       sp.SPNumber, sp.ServiceTag, sp.SPID, sp.CorpID,
                       sp.IP, sp.ListenPort, sp.LoginName, sp.Password,
                       sp.KeyID);

            bool bResult = false;
            _cmd.Connection.Open();
            try
            {
                bResult = (_cmd.ExecuteNonQuery() == 1);
            }
            finally
            {
                _cmd.Connection.Close();
               
            }

            return bResult;
        }

        /// <summary>
        /// 从[subSP]表中删除记录
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public bool Delete(SubSP sp)
        {
            _cmd.CommandText = string.Format("delete from [subSP] where DBkeyID = {0}", sp.KeyID);
            _cmd.Connection.Open();
            bool bResult = false;
            try
            {
                bResult = (_cmd.ExecuteNonQuery() == 1);
            }
            finally
            {
                _cmd.Connection.Close();
                if (bResult)
                {
                    lock (this)
                    {
                        _spList.Remove(sp);
                    }
                }
            }

            return bResult;
        }

        public  SubSP FindByLoginName(string str)
        {
            lock (this)
            {
                SubSP result = _spList.Find(delegate(SubSP sp) { return (sp.LoginName == str); });
                return result;
            }            
        }

        public SubSP FindBySPNumber(string str)
        {
            lock (this)
            {
                SubSP result = _spList.Find(delegate(SubSP sp) { return (sp.SPNumber == str); });
                return result;
            }
        }

        public  SubSP FindBySPID(string str)
        {
            lock (this)
            {
                SubSP result = _spList.Find(delegate(SubSP sp) { return (sp.SPID == str); });
                return result;
            }            
        }

        public  SubSP FindByCorpID(string str)
        {
            lock (this)
            {
                SubSP result = _spList.Find(delegate(SubSP sp) { return (sp.CorpID == str); });
                return result;
            }            
        }

        /// <summary>
        /// 获得二级sp的个数
        /// </summary>
        public int Count
        {
            get { return _spList.Count; }
        }
    }
     */
    #endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -