C# 重新使用类连接到第二个数据库

C# 重新使用类连接到第二个数据库,c#,asp.net,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,

我有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=newsqlnewdatabaseconnmgr()

为了实现这一点,我创建了另一个类并将
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()”:找不到合适的方法 凌驾


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


您可以将SetConnection()设置为受保护和虚拟,并在SQLNewDatabaseConnMgr上重写它以使用不同的值。您可能还需要重写_Server属性。请参见中的示例。如何重写它?我对c#有点生疏,你能给我一个快速的例子或文档说明我如何实现它吗@HubertJarema实际上,您可能希望将一些代码提升到一个公共抽象基类中,然后有两个子类,一个模仿当前类,另一个模仿新类。这样,类之间的唯一区别是一组特定于每个类的受保护的可重写属性。这只是@HubertJarema建议的一个变体顺便说一句,我对这句话很感兴趣:
This.\u Server=(string)reader.GetValue(“DBServer”,This.\u Server.GetType())
\u服务器
属性是根据自身的值计算出来的?现在在调用中如何将方法调用到控制器中,例如:
var db=new SQLConnMgr()我得到:无法创建抽象类或接口'SQLConnMgr'的实例@jonathan对,现在您有了两个新类,因此可以使用
var db=new SQLNewDatabaseConnMgr1()。每个派生类都可以描述如何在其覆盖属性中获取连接参数。
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";
}