我的初衷是这样的:在多线程环境下,每个数据库编号对应一个DbHelper对象。

下面是代码,不知道这样写有什么问题。

 namespace TestDAL
 {
     public class DB
     {
         private static string[] ConnString = new[]
         {
             "unknown", "Data Source=163.163.1.100;Initial Catalog=xiaomi;User Id=sa;Password=1234567890;",
             "Data Source=163.163.1.101;Initial Catalog=xiaomi;User Id=sa;Password=1234567890;"
         };
         private static readonly ConcurrentDictionary<int, DbHelperSQLP> concurentDictionary;
         static DB()
         {
             concurentDictionary = new ConcurrentDictionary<int, DbHelperSQLP>();
         }
         private DB()
         {
         }
         public static DbHelperSQLP GetDBHelper(int id)
         {
             if (!concurentDictionary.ContainsKey(id))
                 concurentDictionary.TryAdd(id, new DbHelperSQLP(ConnString[id]));
             DbHelperSQLP db;
             bool result = concurentDictionary.TryGetValue(id, out db);
             return result ? db : null;
         }
     }
 }

调用方法是:

         /// <summary>
         /// 得到一个对象实体
         /// </summary>
         public xiaomi GetModel(int dbno, int id)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("select  top 1 id,username,password,email,ip from xiaomi ");
             strSql.Append(" where id=@id");
             SqlParameter[] parameters = {
                     )
             };
             parameters[].Value = id;
             DbHelperSQLP helper = DB.GetDBHelper(dbno);
             if (helper == null)
                 throw new SqlNullValueException("没有找到数据库地址");
             DataSet ds = helper.Query(strSql.ToString(), parameters);
             ].Rows.Count >  ? DataRowToModel(ds.Tables[].Rows[]) : null;
         }

最新文章

  1. [WCF]DomainServices客户端操作异常处理
  2. Ajax返回中文乱码问题(未解决)
  3. NeHe OpenGL教程 第四十三课:FreeType库
  4. jQuery经典面试题及答案精选[转载]
  5. 【Delphi】无标题移动窗体
  6. JS 获取元素的属性值,非内联样式
  7. Jquery Validate 正则表达式实用验证代码常用的
  8. Android简易实战教程--第十五话《在外部存储中读写文件》
  9. BZOJ_3555_[Ctsc2014]企鹅QQ_哈希
  10. 解决React通过ajax加载数据更新页面不加判断会报错的问题
  11. 网上找的Backbone.js
  12. Python 生成器, 迭代器, 可迭代对象的区别
  13. 每日英语:The Delicate Protocol Of Hugging
  14. [转]ORA-28001: the password has expired解决方法
  15. 洛谷 3379 最近公共祖先(LCA 倍增)
  16. HTML常用标签-&lt;head&gt;内常用标签
  17. 【转】Excel-VBA操作文件四大方法之三
  18. Android Studio Share Project On Github
  19. openMP编程(上篇)之并行程序设计
  20. JetBrains PyCharm(Community版本)的下载、安装和初步使用

热门文章

  1. 一篇文章看懂spark 1.3+各版本特性
  2. PHP学习之[第08讲]数据库MySQL基础之增删改查
  3. 全情投入是做好工作的基础——Leo鉴书39
  4. .net自定义控件
  5. android string.xml里的空格字符
  6. Queueing in the Linux Network Stack !!!!!!!!!!!!!!!
  7. webpack入门学习
  8. Linux特殊权限
  9. 四、分离T4引擎
  10. 抓取锁的sql语句-第二次修改