Re-use class to connect to second database
I have sql connection manager to connect to my database like:
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();
}
}
So when I want to use database I just call this class like this:
var db = new SQLConnMgr();
Then I can call methods inside this like:
db.GetTableBySQL($"exec usp_Reseller_Get");
My question is, how can I re-use this methods into another class to call another database, I mean, instead to use:
var db = new SQLConnMgr();
Now use var bd = new SQLNewDatabaseConnMgr();
To achieve that, I create another class and inherit SQLConnMgr
into it like
public class SQLNewDatabaseConnMgr: SQLDataMgr
{
}
But now how can I call methods of my working class to set up new connection? Regards
UPDATE
As comment bellow I set SetConnection()
method to protected virtual
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());
}
Then in my new class I try
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;
}
}
But method return me error:
'SQLConnAllOrdersMgr.SetConnection()': no suitable method found to
override
c# asp.net
|
show 6 more comments
I have sql connection manager to connect to my database like:
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();
}
}
So when I want to use database I just call this class like this:
var db = new SQLConnMgr();
Then I can call methods inside this like:
db.GetTableBySQL($"exec usp_Reseller_Get");
My question is, how can I re-use this methods into another class to call another database, I mean, instead to use:
var db = new SQLConnMgr();
Now use var bd = new SQLNewDatabaseConnMgr();
To achieve that, I create another class and inherit SQLConnMgr
into it like
public class SQLNewDatabaseConnMgr: SQLDataMgr
{
}
But now how can I call methods of my working class to set up new connection? Regards
UPDATE
As comment bellow I set SetConnection()
method to protected virtual
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());
}
Then in my new class I try
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;
}
}
But method return me error:
'SQLConnAllOrdersMgr.SetConnection()': no suitable method found to
override
c# asp.net
2
You can make SetConnection() protected and virtual and override it on SQLNewDatabaseConnMgr to use different values. You will probably also need to override the _Server property.
– Hubert Jarema
Nov 26 '18 at 22:15
1
See example in ConfigurationManager.ConnectionStrings Property.
– Olivier Jacot-Descombes
Nov 26 '18 at 22:20
How can I override it? I'm a little green in c#, can you give me fast example or documentation of how can I achieve it? @HubertJarema
– Jonathan
Nov 26 '18 at 22:24
Actually, you may want to lift some of your code into a common abstract base class, and then have two subclasses, one that mimics your current class, and a second for your new class. That way, the only difference between the classes is a set of protected overridable properties specific to each class. This is just a variation on what @HubertJarema suggested
– Flydog57
Nov 26 '18 at 22:41
By the way, I'm intrigued by this statement:this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
. The_Server
property is calculated from the value of itself?
– Flydog57
Nov 26 '18 at 22:43
|
show 6 more comments
I have sql connection manager to connect to my database like:
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();
}
}
So when I want to use database I just call this class like this:
var db = new SQLConnMgr();
Then I can call methods inside this like:
db.GetTableBySQL($"exec usp_Reseller_Get");
My question is, how can I re-use this methods into another class to call another database, I mean, instead to use:
var db = new SQLConnMgr();
Now use var bd = new SQLNewDatabaseConnMgr();
To achieve that, I create another class and inherit SQLConnMgr
into it like
public class SQLNewDatabaseConnMgr: SQLDataMgr
{
}
But now how can I call methods of my working class to set up new connection? Regards
UPDATE
As comment bellow I set SetConnection()
method to protected virtual
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());
}
Then in my new class I try
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;
}
}
But method return me error:
'SQLConnAllOrdersMgr.SetConnection()': no suitable method found to
override
c# asp.net
I have sql connection manager to connect to my database like:
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();
}
}
So when I want to use database I just call this class like this:
var db = new SQLConnMgr();
Then I can call methods inside this like:
db.GetTableBySQL($"exec usp_Reseller_Get");
My question is, how can I re-use this methods into another class to call another database, I mean, instead to use:
var db = new SQLConnMgr();
Now use var bd = new SQLNewDatabaseConnMgr();
To achieve that, I create another class and inherit SQLConnMgr
into it like
public class SQLNewDatabaseConnMgr: SQLDataMgr
{
}
But now how can I call methods of my working class to set up new connection? Regards
UPDATE
As comment bellow I set SetConnection()
method to protected virtual
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());
}
Then in my new class I try
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;
}
}
But method return me error:
'SQLConnAllOrdersMgr.SetConnection()': no suitable method found to
override
c# asp.net
c# asp.net
edited Nov 26 '18 at 22:45
Jonathan
asked Nov 26 '18 at 22:11
JonathanJonathan
3248
3248
2
You can make SetConnection() protected and virtual and override it on SQLNewDatabaseConnMgr to use different values. You will probably also need to override the _Server property.
– Hubert Jarema
Nov 26 '18 at 22:15
1
See example in ConfigurationManager.ConnectionStrings Property.
– Olivier Jacot-Descombes
Nov 26 '18 at 22:20
How can I override it? I'm a little green in c#, can you give me fast example or documentation of how can I achieve it? @HubertJarema
– Jonathan
Nov 26 '18 at 22:24
Actually, you may want to lift some of your code into a common abstract base class, and then have two subclasses, one that mimics your current class, and a second for your new class. That way, the only difference between the classes is a set of protected overridable properties specific to each class. This is just a variation on what @HubertJarema suggested
– Flydog57
Nov 26 '18 at 22:41
By the way, I'm intrigued by this statement:this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
. The_Server
property is calculated from the value of itself?
– Flydog57
Nov 26 '18 at 22:43
|
show 6 more comments
2
You can make SetConnection() protected and virtual and override it on SQLNewDatabaseConnMgr to use different values. You will probably also need to override the _Server property.
– Hubert Jarema
Nov 26 '18 at 22:15
1
See example in ConfigurationManager.ConnectionStrings Property.
– Olivier Jacot-Descombes
Nov 26 '18 at 22:20
How can I override it? I'm a little green in c#, can you give me fast example or documentation of how can I achieve it? @HubertJarema
– Jonathan
Nov 26 '18 at 22:24
Actually, you may want to lift some of your code into a common abstract base class, and then have two subclasses, one that mimics your current class, and a second for your new class. That way, the only difference between the classes is a set of protected overridable properties specific to each class. This is just a variation on what @HubertJarema suggested
– Flydog57
Nov 26 '18 at 22:41
By the way, I'm intrigued by this statement:this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
. The_Server
property is calculated from the value of itself?
– Flydog57
Nov 26 '18 at 22:43
2
2
You can make SetConnection() protected and virtual and override it on SQLNewDatabaseConnMgr to use different values. You will probably also need to override the _Server property.
– Hubert Jarema
Nov 26 '18 at 22:15
You can make SetConnection() protected and virtual and override it on SQLNewDatabaseConnMgr to use different values. You will probably also need to override the _Server property.
– Hubert Jarema
Nov 26 '18 at 22:15
1
1
See example in ConfigurationManager.ConnectionStrings Property.
– Olivier Jacot-Descombes
Nov 26 '18 at 22:20
See example in ConfigurationManager.ConnectionStrings Property.
– Olivier Jacot-Descombes
Nov 26 '18 at 22:20
How can I override it? I'm a little green in c#, can you give me fast example or documentation of how can I achieve it? @HubertJarema
– Jonathan
Nov 26 '18 at 22:24
How can I override it? I'm a little green in c#, can you give me fast example or documentation of how can I achieve it? @HubertJarema
– Jonathan
Nov 26 '18 at 22:24
Actually, you may want to lift some of your code into a common abstract base class, and then have two subclasses, one that mimics your current class, and a second for your new class. That way, the only difference between the classes is a set of protected overridable properties specific to each class. This is just a variation on what @HubertJarema suggested
– Flydog57
Nov 26 '18 at 22:41
Actually, you may want to lift some of your code into a common abstract base class, and then have two subclasses, one that mimics your current class, and a second for your new class. That way, the only difference between the classes is a set of protected overridable properties specific to each class. This is just a variation on what @HubertJarema suggested
– Flydog57
Nov 26 '18 at 22:41
By the way, I'm intrigued by this statement:
this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
. The _Server
property is calculated from the value of itself?– Flydog57
Nov 26 '18 at 22:43
By the way, I'm intrigued by this statement:
this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
. The _Server
property is calculated from the value of itself?– Flydog57
Nov 26 '18 at 22:43
|
show 6 more comments
1 Answer
1
active
oldest
votes
You can override the properties which indicate the settings to use for the database connection. I think it's simpler than asking the developer to remember how to retrieve the settings (i.e. when marking SetConnection
as virtual or abstract)
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";
}
And how can I call methods into my controllers now in my calls like:var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'
– Jonathan
Nov 26 '18 at 22:56
@jonathan right, well now you have two new classes so you would usevar db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.
– djv
Nov 27 '18 at 2:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489907%2fre-use-class-to-connect-to-second-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can override the properties which indicate the settings to use for the database connection. I think it's simpler than asking the developer to remember how to retrieve the settings (i.e. when marking SetConnection
as virtual or abstract)
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";
}
And how can I call methods into my controllers now in my calls like:var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'
– Jonathan
Nov 26 '18 at 22:56
@jonathan right, well now you have two new classes so you would usevar db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.
– djv
Nov 27 '18 at 2:38
add a comment |
You can override the properties which indicate the settings to use for the database connection. I think it's simpler than asking the developer to remember how to retrieve the settings (i.e. when marking SetConnection
as virtual or abstract)
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";
}
And how can I call methods into my controllers now in my calls like:var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'
– Jonathan
Nov 26 '18 at 22:56
@jonathan right, well now you have two new classes so you would usevar db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.
– djv
Nov 27 '18 at 2:38
add a comment |
You can override the properties which indicate the settings to use for the database connection. I think it's simpler than asking the developer to remember how to retrieve the settings (i.e. when marking SetConnection
as virtual or abstract)
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";
}
You can override the properties which indicate the settings to use for the database connection. I think it's simpler than asking the developer to remember how to retrieve the settings (i.e. when marking SetConnection
as virtual or abstract)
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";
}
answered Nov 26 '18 at 22:48
djvdjv
7,96973252
7,96973252
And how can I call methods into my controllers now in my calls like:var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'
– Jonathan
Nov 26 '18 at 22:56
@jonathan right, well now you have two new classes so you would usevar db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.
– djv
Nov 27 '18 at 2:38
add a comment |
And how can I call methods into my controllers now in my calls like:var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'
– Jonathan
Nov 26 '18 at 22:56
@jonathan right, well now you have two new classes so you would usevar db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.
– djv
Nov 27 '18 at 2:38
And how can I call methods into my controllers now in my calls like:
var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'– Jonathan
Nov 26 '18 at 22:56
And how can I call methods into my controllers now in my calls like:
var db = new SQLConnMgr();
I'm getting: Cannot create an instance of the abstract class or interface 'SQLConnMgr'– Jonathan
Nov 26 '18 at 22:56
@jonathan right, well now you have two new classes so you would use
var db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.– djv
Nov 27 '18 at 2:38
@jonathan right, well now you have two new classes so you would use
var db = new SQLNewDatabaseConnMgr1();
. Each of those derived classes can describe how to get the connection parameters in their override properties.– djv
Nov 27 '18 at 2:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489907%2fre-use-class-to-connect-to-second-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You can make SetConnection() protected and virtual and override it on SQLNewDatabaseConnMgr to use different values. You will probably also need to override the _Server property.
– Hubert Jarema
Nov 26 '18 at 22:15
1
See example in ConfigurationManager.ConnectionStrings Property.
– Olivier Jacot-Descombes
Nov 26 '18 at 22:20
How can I override it? I'm a little green in c#, can you give me fast example or documentation of how can I achieve it? @HubertJarema
– Jonathan
Nov 26 '18 at 22:24
Actually, you may want to lift some of your code into a common abstract base class, and then have two subclasses, one that mimics your current class, and a second for your new class. That way, the only difference between the classes is a set of protected overridable properties specific to each class. This is just a variation on what @HubertJarema suggested
– Flydog57
Nov 26 '18 at 22:41
By the way, I'm intrigued by this statement:
this._Server = (string)reader.GetValue("DBServer", this._Server.GetType());
. The_Server
property is calculated from the value of itself?– Flydog57
Nov 26 '18 at 22:43