导航

数据库操作基类的连接字符串web.config获取

Posted on 2006-08-04 00:14  Harbor  阅读(1677)  评论(2)    收藏  举报

最近使用了一个数据库操作的基类,把连接字符串的获取,设置成从web.config中获取,appsetting中的对应节的名称是固定的。

现在我要使用这个基类在同一个网站中,做两个系统,不同的数据库不同的连接字符串,如何让它们共存于一个web.config文件中而不修改基类的函数实现。或者如何改进基类的连接字符串的获取函数设置来实现完全的通用???
部分代码如下:
public abstract class DbBase
    {
        public abstract IDbConnection Connection
        {
            get;
        } //得到数据库连接
 public static DBOperator Instance(string strConnection)
        {
            if (strConnection.IndexOf("provider=") < 0) //SqlServer
            {
                return new SqlDBOperator(strConnection);
            }
            else //other database
            {
                return null;
                //new System.Data.OleDBOperator(strConnection);
          }
  }

/// <summary>
    /// 继承自DBOperator的SqlDBOperator类是一个SQLserver数据库提供者的数据操作类.
    /// </summary>
    public class SqlDBOperator : DbBase
    {
        private SqlConnection myConnection; //数据库连接
 //属性----通用数据连接接口
        public override IDbConnection Connection
        {
            get
            {
                return myConnection;
            }

        }

        //类的构造函数,通过一个连接字符串来实例化该类.
        public SqlDBOperator(string strConnection)
        {
            this.myConnection = new SqlConnection(strConnection);
        }
        public SqlDBOperator()
        {
            this.myConnection = new SqlConnection(GetConnString());

        }

        //静态,返回数据库连接串
        protected string GetConnString()
        {
            return System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
        }
}