本站首页    管理页面    写新日志    退出 [QQ:172832876] [MSN:lanlanq@hotmail.com]
麒麟在天欢迎您

.:日期

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.:我的分类

.:最新日志

.:显示信息

blog名称:
日志总数:64
评论数量:34
留言数量:3
访问次数:343227
建立时间:2006年3月10日

.:留言板

.:链接

        公告

在属于自己的一片天空中,敲打着键盘,记录下自己感兴趣和认为自己因该回忆的碎片,可能是财富,也可能是不足,过后你会发现你的进步,这就足够。
[C++/C#(DotNet)]c# 数据库连接管理类
luckystar 发表于 2010/2/25 10:26:47

C# 数据库连接管理类,设置连接个数,统一管理数据连接通道   class DbconManager    {        private static DbconManager theSingleton = null;        private DBOper db = null;        DBOper[] ConnectionPool = new DBOper[Properties.Settings.Default.PoolCount];        int CurrentPosition = -1;         private DbconManager() { }         public static DbconManager newInstance()        {            if (null == theSingleton)            {                theSingleton = new DbconManager();            }            return theSingleton;        }                 public DBOper getDbConnectionFactory()        {            try            {                if (db == null)                {                    db = getDBOper(Properties.Settings.Default.ConnectionString);                }                else                {                    if (db.isBroken())                    {                        db.Close();                        db.Open();                    }                    else if (db.isClose())                    {                        db.Open();                    }                }                return db;            }            catch (Exception ex)            {                throw new e.HDbException(ex.Message, "dbmanager003");            }                    }                 public DBOper getOtherDbConnectionFactory(String connectString)        {            try            {                db = DBOper(connectString);                if (db == null)                {                    throw new e.HDbException("连接字符串ConnetionString有问题,请核实,连接对象为空!", "dbmanager001");                }                else                {                    if (db.isBroken())                    {                        db.Close();                        db.Open();                    }                    else if (db.isClose())                    {                        db.Open();                    }                }                return db;            }            catch (Exception ex)            {                throw new e.HDbException(ex.Message, "dbmanager002");            }        }                 public DBOper getDBOper(string strConnection)        {            try            {                if (fhvmj.Properties.Settings.Default.PoolCount < 1) //没有缓冲                 {                    return CreateNewDBOper(strConnection);                }                else                {                    CurrentPosition++;                    if (CurrentPosition == fhvmj.Properties.Settings.Default.PoolCount)                        CurrentPosition = 0;                    if (ConnectionPool[CurrentPosition] == null)                    {                        ConnectionPool[CurrentPosition] = CreateNewDBOper(strConnection);                    }                    return ConnectionPool[CurrentPosition];                }            }            catch (Exception e)            {                Logger.info("debug getDBOperator:" + e.Message);                // 向上抛出系统异常                throw new e.HDbException(e.Message, e, "dbmanager003");            }        }                 public DBOper CreateNewDBOper(string strConnection)        {            if (strConnection.IndexOf("Provider=") > -1) // ole             {                return new Ole(strConnection);            }            if (strConnection.IndexOf("Data Source=") > -1) // SqlServer             {                return new Sqldb(strConnection);            }            if (strConnection.IndexOf("Dsn=") > -1) // odbc            {                return new Odbc(strConnection);            }            if (strConnection.IndexOf("server=") > -1) // mysql            {                return new Mysql(strConnection);            }            return null;        }    }

阅读全文(3217) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144760109 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号