base class connection string
-
Hi, I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this. With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application. One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated. Thanks for any suggestions.
-
Hi, I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this. With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application. One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated. Thanks for any suggestions.
That's done in the Data Access Layer. Are you saying you do it in your business objects?
-
Hi, I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this. With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application. One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated. Thanks for any suggestions.
class base
{
string m_connString;
public string ConnString
{
get{ return m_ConnString;}
private set{ m_ConnString = value;
}public Base()
{
ConnString = default_value
}pubic Base(string connString)
{
ConnString = connString;
}
}class Derived : Base
{
pubic Derived(string connString) : base(connString)
{}
}
I know the language. I've read a book. - _Madmatt
-
That's done in the Data Access Layer. Are you saying you do it in your business objects?
-
Nope, this is in a base class for the Data Access Layer code objects. Sorry if my post was not clear.
Good. In my data access base classes[^] I hold a
System.Data.IDbCommand
, if I want the connection string I usethis.cmd.Connection.ConnectionString
; there's no need to store it a second time. I don't keep instantiating and throwing away connection and command instances -- that's wasteful (not that I've benchmarked it). But now I don't know what you mean by "each object can access the database independently". What objects? I have one data access class that is basically a wrapper around one Connection/Command (with specialized versions for the various databases I use). I can instantiate one for each database and I can clone it when I need an additional connection to a particular database.