gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:48 25 4
gpt4 key购买 nike

我有 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 虚拟

 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()': no suitable method found to override

最佳答案

您可以覆盖指示用于数据库连接的设置的属性。我认为这比要求开发人员记住如何检索设置(即在将 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";
}

关于c# - 重新使用类连接到第二个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com