以前设计,无法实现多数据库操作,本次修改做了部分改动,尤其配置文件,加了自定义配置节来处理多数据库
只贴出修改的关键代码,及配置处理,配置样式
关于接口设计等其它相关代码,请参见以前写的〈数据访问层设计〉
1/数据库配置类,管理参数等
using System;
using System.Collections;
namespace HKH.DataBase.Type
{
/// <summary>
/// 数据库访问配置
/// </summary>
/// <remarks>
/// Create By Liwt on 2007 - 03 - 12
/// 保存数据库访问配置的参数
/// </remarks>
public class clsDataBaseConfig
{
#region 私有变量
private string configName;
private bool isDefault = false;
private string m_DAOClass;
private string m_DBClass;
private clsConnParam m_clsConnParam = null;
#endregion
#region 属性
/// <summary>
/// 获取该数据库的连接字符串
/// </summary>
public string ConnectionString
{
get
{
return m_clsConnParam.toConnectionString();
}
}
/// <summary>
/// 配置名称
/// </summary>
internal string ConfigName
{
get
{
return configName;
}
set
{
configName = value;
}
}
/// <summary>
/// 是否缺省数据库
/// </summary>
internal bool IsDefault
{
get
{
return isDefault;
}
set
{
isDefault = value;
}
}
/// <summary>
/// 获取DAO类类型
/// </summary>
internal string DAOClass
{
get
{
return m_DAOClass;
}
set
{
m_DAOClass = value;
}
}
/// <summary>
/// 获取DBA类类型
/// </summary>
internal string DBClass
{
get
{
return m_DBClass;
}
set
{
m_DBClass = value;
}
}
/// <summary>
/// 获取参数集合
/// </summary>
public clsConnParam ConnParam
{
get
{
return m_clsConnParam;
}
}
#endregion
#region 私有方法
#endregion
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public clsDataBaseConfig()
{
this.m_DAOClass = "";
this.m_DBClass = "";
m_clsConnParam = new clsConnParam();
}
#endregion
}
/// <summary>
/// 数据库连接参数集合
/// </summary>
public class clsConnParam
{
private Hashtable m_Params = null;
public clsConnParam()
{
m_Params = new Hashtable();
}
/// <summary>
/// 连接参数索引器
/// </summary>
public string this[object index]
{
get
{
return m_Params[index].ToString();
}
set
{
m_Params[index] = value;
}
}
/// <summary>
/// 获取参数总数
/// </summary>
public int Count
{
get
{
return this.m_Params.Count;
}
}
/// <summary>
/// 添加新参数
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Add ( object key,object value )
{
if ( !ContainsParam(key) )
this.m_Params.Add(key,value);
}
/// <summary>
/// 删除参数
/// </summary>
/// <param name="key"></param>
public void Remove ( object key )
{
if ( ContainsParam(key) )
this.m_Params.Remove(key);
}
/// <summary>
/// 测试是否存在特定的参数
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsParam(object key)
{
return this.m_Params.ContainsKey(key);
}
/// <summary>
/// 生成连接字符串
/// </summary>
/// <returns>连接字符串</returns>
internal string toConnectionString()
{
System.Text.StringBuilder szConn = new System.Text.StringBuilder();
IDictionaryEnumerator ide = m_Params.GetEnumerator();
ide.Reset();
while ( ide.MoveNext() )
{
szConn.Append(ide.Key);
szConn.Append("=");
szConn.Append(ide.Value);
szConn.Append(";");
}
return szConn.ToString();
}
}
}
2、数据访问配置类,管理多数据库配置
using System;
using System.Data;
using System.Xml;
using System.Collections;
using System.Configuration;
using HKH.DataBase;
using HKH.DataBase.Type;
namespace HKH.DataBase.Config
{
/// <summary>
/// 数据访问层的配置类,单例模式
/// </summary>
/// <remarks>
/// Create By Liwt on 2007 - 03 - 12
/// </remarks>
public class clsDBAccessConfig
{
//保持对自己的静态引用
private static clsDBAccessConfig m_clsDBAccessConfig = new clsDBAccessConfig();
#region 私有变量
private Hashtable m_DBSettings = null;
private string m_DefaultDBSetting = null;
#endregion
#region 属性
/// <summary>
/// 控制访问的缺省数据配置设置
/// </summary>
internal string DefaultDBSetting
{
get
{
if ( null == this.m_DBSettings || 0 == m_DBSettings.Count )
{
this.ReadDBConfig();
}
return m_DefaultDBSetting;
}
set
{
m_DefaultDBSetting = value;
}
}
/// <summary>
/// 获取参数总数
/// </summary>
public int DBConfigCount
{
get
{
return this.m_DBSettings.Count;
}
}
#endregion
#region 公共方法
/// <summary>
/// 获取默认配置
/// </summary>
/// <returns></returns>
public clsDataBaseConfig GetDBConfig()
{
try
{
if ( null == this.m_DBSettings || 0 == m_DBSettings.Count )
{
this.ReadDBConfig();
}
if ( null == m_DefaultDBSetting || "" == m_DefaultDBSetting.Trim() )
throw new Exception("没有配置默认数据库!");
return (clsDataBaseConfig)m_DBSettings[m_DefaultDBSetting];
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取配置
/// </summary>
/// <param name="strConfigName"></param>
/// <returns></returns>
public clsDataBaseConfig GetDBConfig(string strConfigName)
{
try
{
if ( null == this.m_DBSettings || 0 == m_DBSettings.Count )
{
this.ReadDBConfig();
}
return (clsDataBaseConfig)m_DBSettings[strConfigName];
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// 添加新配置
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddConfig ( object key,object value )
{
if ( !ContainsConfig(key) )
this.m_DBSettings.Add(key,value);
}
/// <summary>
/// 删除配置
/// </summary>
/// <param name="key"></param>
public void RemoveConfig ( object key )
{
if ( ContainsConfig(key) )
this.m_DBSettings.Remove(key);
}
/// <summary>
/// 是否存在特定的配置
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsConfig(object key)
{
return this.m_DBSettings.ContainsKey(key);
}
#endregion
/// <summary>
/// 私有构造
/// </summary>
private clsDBAccessConfig()
{
m_DBSettings = new Hashtable();
}
/// <summary>
/// 对外提供自身实例
/// </summary>
/// <returns></returns>
public static clsDBAccessConfig GetInstance()
{
return m_clsDBAccessConfig;
}
#region 私有方法
/// <summary>
/// 读取配置文件
/// </summary>
/// <returns></returns>
private void ReadDBConfig()
{
try
{
//获取设置,实际处理在 配置节处理程序中
System.Configuration.ConfigurationSettings.GetConfig("DBSettings/DBSetting");
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}