重用类连接到第二个数据库

时间:2018-11-26 22:11:35

标签: c# asp.net

我有sql连接管理器,可以连接到我的数据库,例如:

public class SQLConnMgr : Disposable {
    #region private properties
    SqlConnection dbconn = new SqlConnection();
    private bool _Errors;

  private string
         _ErrMsg = string.Empty,
         _Catalog = string.Empty,
         _Server = string.Empty,
         _UserID = string.Empty,
         _Pwd = string.Empty,
         _ConnStr = string.Empty;
    public SQLConnMgr()
    {
        this.SetConnection();
        this.InitClass();
    }

    private void SetConnection()
    {
        AppSettingsReader reader = new AppSettingsReader();
        this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
        this._Catalog = (string)reader.GetValue("DBCatalog", this._Catalog.GetType());
        this._UserID = (string)reader.GetValue("DBUser", this._UserID.GetType());
        this._Pwd = (string)reader.GetValue("DBPwd", this._Pwd.GetType());
    }


   private void InitClass()
        {
            if (this._ConnStr == string.Empty)
            {
                System.Text.StringBuilder cn = new System.Text.StringBuilder();
                cn.AppendFormat("Server={0};initial catalog={1};", this._Server, this._Catalog);
                cn.AppendFormat("user id={0};password={1};persist security info=True;packet size=4096;Connect Timeout=120", this._UserID, this._Pwd);
                dbconn.ConnectionString = cn.ToString();
            }
            else
            {
                dbconn.ConnectionString = this._ConnStr;
            }
            try
            {
                // open connection to SQL
                dbconn.Open();
                if (dbconn.State != ConnectionState.Open)
                {
                    this._Errors = true;
                    this._ErrMsg = "Connection State is not open!";
                }
            }
            catch (System.InvalidOperationException ex)
            {
                this._ErrMsg = ex.Message;
                this._ErrMsg = string.Empty;
                //added 1/12/2010 - Johan
                SqlConnection.ClearPool(dbconn);
                SqlConnection.ClearAllPools();
                // attempt the connection again?
                dbconn.Close();
                InitClass();
            }
            catch (Exception e)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("Error occured while attempting connect to the database");
                sb.Append(e.Message.ToString());
                sb.Append(e.Source.ToString());
                sb.Append(e.TargetSite.ToString());
                this._ErrMsg = sb.ToString();
            }
        }

因此,当我想使用数据库时,只需像这样调用此类:

var db = new SQLConnMgr();

然后我可以像这样调用内部的方法:

db.GetTableBySQL($"exec usp_Reseller_Get");

我的问题是,我该如何在另一个类中重用此方法来调用另一个数据库,而不是使用:

var db = new SQLConnMgr();

现在使用var bd = new SQLNewDatabaseConnMgr();

为此,我创建了另一个类,并像这样继承SQLConnMgr

public class SQLNewDatabaseConnMgr: SQLDataMgr
{

}

但是现在我该如何调用工作类的方法来建立新的连接?问候

更新

在下面的评论中,我将SetConnection()方法设置为受保护的虚拟

 protected virtual void SetConnection()
        {
            AppSettingsReader reader = new AppSettingsReader();
            this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
            this._Catalog = (string)reader.GetValue("DBCatalog", this._Catalog.GetType());
            this._UserID = (string)reader.GetValue("DBUser", this._UserID.GetType());
            this._Pwd = (string)reader.GetValue("DBPwd", this._Pwd.GetType());
        }

然后在我的新课上尝试

   public class SQLNewDatabaseConnMgr: SQLDataMgr
        {
            private string
             _ErrMsg = string.Empty,
             _Catalog = string.Empty,
             _Server = string.Empty,
             _UserID = string.Empty,
             _Pwd = string.Empty,
             _ConnStr = string.Empty;

            public override bool SetConnection()
            {
                AppSettingsReader reader = new AppSettingsReader();
                this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
                this._Catalog = (string)reader.GetValue("DBCatalog", this._Catalog.GetType());
                this._UserID = (string)reader.GetValue("DBUser", this._UserID.GetType());
                this._Pwd = (string)reader.GetValue("DBPwd", this._Pwd.GetType());
                return true;
            }
        }

但是方法返回我错误:

  

'SQLConnAllOrdersMgr.SetConnection()':找不到合适的方法来   覆盖

1 个答案:

答案 0 :(得分:1)

您可以覆盖指示数据库连接要使用的设置的属性。我认为这比要求开发人员记住如何检索设置(即将SetConnection标记为虚拟或抽象时)更简单

public abstract class SQLConnMgr : Disposable
{

    SqlConnection dbconn = new SqlConnection();

    protected abstract string DBServer { get;  }
    protected abstract string DBCatalog { get;  }
    protected abstract string DBUser { get;  }
    protected abstract string DBPwd { get;  }

    protected string _Server;
    protected string _Catalog;
    protected string _UserID;
    protected string _Pwd;

    public SQLConnMgr()
    {
        this.SetConnection();
        this.InitClass();
    }

    protected void SetConnection()
    {
        AppSettingsReader reader = new AppSettingsReader();
        this._Server = (string)reader.GetValue(this.DBServer, this._Server.GetType());
        this._Catalog = (string)reader.GetValue(this.DBCatalog, this._Catalog.GetType());
        this._UserID = (string)reader.GetValue(this.DBUser, this._UserID.GetType());
        this._Pwd = (string)reader.GetValue(this.DBPwd, this._Pwd.GetType());
    }

}

public class SQLNewDatabaseConnMgr1 : SQLConnMgr
{
    protected override string DBServer => "DBServer1";
    protected override string DBCatalog => "DBCatalog1";
    protected override string DBUser => "DBUser1";
    protected override string DBPwd => "DBPwd1";
}

public class SQLNewDatabaseConnMgr2 : SQLConnMgr
{
    protected override string DBServer => "DBServer2";
    protected override string DBCatalog => "DBCatalog2";
    protected override string DBUser => "DBUser2";
    protected override string DBPwd => "DBPwd2";
}