C# ASP.NET 通用权限管理系统组件的数据访问层的调用方法参考2 - 多种类型的多数据库连接方法
2013-02-03 10:44 通用C#系统架构 阅读(779) 评论(0) 编辑 收藏 举报往往一个网站需要同时连接多个数据库,例如 用户中心库、消息库、业务库、审批流程库、论坛等等n多数据库,而且很可能还是多种类型的数据库,所以我们需要有一个灵活的数据库访问组件。下面我们看看通用权限管理系统组件里的数据库访问连接库的用法,例子代码是连接4个数据库的实际例子。
//-------------------------------------------------------------------- // All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. //-------------------------------------------------------------------- using System; using System.Data; namespace DotNet.WebForm.Example { using DotNet.Utilities; /// <remarks> /// MultiDatabase /// /// 修改纪录 /// /// 版本:1.0 2013.02.03 JiRiGaLa 创建。 /// /// 版本:1.0 /// <author> /// <name>JiRiGaLa</name> /// <date>2013.02.03</date> /// </author> /// </remarks> public partial class MultiDatabase : System.Web.UI.Page { /// <summary> /// 同时多数据库连接的方法。 /// /// 优点: /// 同时可以连接多个数据库 /// </summary> private void UseMultiDatabase() { // 当然这个数据库连接也可以从配置文件获取,这里是简单的例子,直接写入数据库连接串了 // 用户中心的数据库连接 BaseSystemInfo.UserCenterDbConnection = "Data Source=localhost;Initial Catalog=UserCenterV37;User Id = sa ; Password = Password@sa;max pool size=50;min pool size=10"; // 业务数据库连接 BaseSystemInfo.BusinessDbConnection = "Data Source=localhost;Initial Catalog=ProjectV37;User Id = sa ; Password = Password@sa;max pool size=50;min pool size=10"; // 审批流程数据库连接 BaseSystemInfo.WorkFlowDbConnection = "Data Source=localhost;Initial Catalog=WorkFlowV37;User Id = sa ; Password = Password@sa;max pool size=50;min pool size=10"; // 消息中心数据库连接 BaseSystemInfo.MessageDbConnection = "Data Source=localhost;Initial Catalog=MessageCenterV37;User Id = sa ; Password = Password@sa;max pool size=50;min pool size=10"; // 用户中心的数据库连接 IDataReader dataReader1 = DbHelper.ExecuteReader(BaseSystemInfo.UserCenterDbConnection, "SELECT Id FROM BaseUser"); while (dataReader1.Read()) { } dataReader1.Close(); DbHelper.ExecuteNonQuery(BaseSystemInfo.UserCenterDbConnection, "SELECT COUNT(1) FROM BaseUser"); DbHelper.Fill(BaseSystemInfo.UserCenterDbConnection, "SELECT Id FROM BaseUser"); DbHelper.ExecuteScalar(BaseSystemInfo.UserCenterDbConnection, "SELECT COUNT(1) FROM BaseUser"); // 业务数据库连接 IDataReader dataReader2 = DbHelper.ExecuteReader(BaseSystemInfo.BusinessDbConnection, "SELECT Id FROM Project"); while (dataReader2.Read()) { } dataReader2.Close(); DbHelper.ExecuteNonQuery(BaseSystemInfo.BusinessDbConnection, "SELECT COUNT(1) FROM Project"); DbHelper.Fill(BaseSystemInfo.BusinessDbConnection, "SELECT Id FROM Project"); DbHelper.ExecuteScalar(BaseSystemInfo.BusinessDbConnection, "SELECT COUNT(1) FROM Project"); // 审批流程数据库连接 IDataReader dataReader3 = DbHelper.ExecuteReader(BaseSystemInfo.WorkFlowDbConnection, "SELECT Id FROM BaseWorkFlowProcess"); while (dataReader3.Read()) { } dataReader3.Close(); DbHelper.ExecuteNonQuery(BaseSystemInfo.WorkFlowDbConnection, "SELECT COUNT(1) FROM BaseWorkFlowProcess"); DbHelper.Fill(BaseSystemInfo.WorkFlowDbConnection, "SELECT Id FROM BaseWorkFlowProcess"); DbHelper.ExecuteScalar(BaseSystemInfo.WorkFlowDbConnection, "SELECT COUNT(1) FROM BaseWorkFlowProcess"); // 消息中心数据库连接 IDataReader dataReader4 = DbHelper.ExecuteReader(BaseSystemInfo.MessageDbConnection, "SELECT Id FROM BaseMessage"); while (dataReader4.Read()) { } dataReader4.Close(); DbHelper.ExecuteNonQuery(BaseSystemInfo.MessageDbConnection, "SELECT COUNT(1) FROM BaseMessage"); DbHelper.Fill(BaseSystemInfo.MessageDbConnection, "SELECT Id FROM BaseMessage"); DbHelper.ExecuteScalar(BaseSystemInfo.MessageDbConnection, "SELECT COUNT(1) FROM BaseMessage"); } protected void Page_Load(object sender, EventArgs e) { // 调用多个数据库连接 UseMultiDatabase(); } } }
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。