一个关于多线程和DbHelper的问题
2024-10-10 06:51:57
我的初衷是这样的:在多线程环境下,每个数据库编号对应一个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; }
最新文章
- [WCF]DomainServices客户端操作异常处理
- Ajax返回中文乱码问题(未解决)
- NeHe OpenGL教程 第四十三课:FreeType库
- jQuery经典面试题及答案精选[转载]
- 【Delphi】无标题移动窗体
- JS 获取元素的属性值,非内联样式
- Jquery Validate 正则表达式实用验证代码常用的
- Android简易实战教程--第十五话《在外部存储中读写文件》
- BZOJ_3555_[Ctsc2014]企鹅QQ_哈希
- 解决React通过ajax加载数据更新页面不加判断会报错的问题
- 网上找的Backbone.js
- Python 生成器, 迭代器, 可迭代对象的区别
- 每日英语:The Delicate Protocol Of Hugging
- [转]ORA-28001: the password has expired解决方法
- 洛谷 3379 最近公共祖先(LCA 倍增)
- HTML常用标签-<;head>;内常用标签
- 【转】Excel-VBA操作文件四大方法之三
- Android Studio Share Project On Github
- openMP编程(上篇)之并行程序设计
- JetBrains PyCharm(Community版本)的下载、安装和初步使用
热门文章
- 一篇文章看懂spark 1.3+各版本特性
- PHP学习之[第08讲]数据库MySQL基础之增删改查
- 全情投入是做好工作的基础——Leo鉴书39
- .net自定义控件
- android string.xml里的空格字符
- Queueing in the Linux Network Stack !!!!!!!!!!!!!!!
- webpack入门学习
- Linux特殊权限
- 四、分离T4引擎
- 抓取锁的sql语句-第二次修改