Multiple classes in C# and one connection string

Jun 2, 2008
163
0
0
I'm working on a project and I have a question.

I and another person are working on it and we have decided to keep classes separate for organization.

So we have dbLoginAuth.cs and dbDataReader.cs classes, there will be more in the future.

They both need to connect to a database.

I'd like to have another class that has the connection string so we have a one stop area that if it needs to be changed, it's in one spot.

dbLoginAuth and dbDataReader have this.

SqlConnection con = null;
SqlCommand com = null;
SqlDataReader dr = null;

con = new SqlConnection(ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString);
con.Open();

etc....

I want them to not have this but call it from another class, is that the right way to approach this? Or should I made like maybe a XML file to read from?


 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Just make a db connector class which has a load of common methods etc, possibly using something like SqlHelper from Microsoft Enterprise library to wrap everything up neatly.
 
Jun 2, 2008
163
0
0
All of your answers went over my head :(

dbLogin.cs looks like this.

****************

public class dbLogin.cs
{
SqlConnection con = null;
SqlCommand com = null;
SqlDataReader dr = null;

//Stored procedure
const string IDcheck = "CheckUser";


public dbLogin()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString);
con.Open();


}

****************

I started dbConn.cs, this is what I have so far

****************

public class dbConn
{
string connStr;



public dbConn()
{
connStr = (ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString);
}

}

****************

Am I able to pass connStr from dbConn.cs to dbLogin.cs?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Sure, just create an access method for it in your dbConn class.

internal string ConnStr {
get { return this.connStr; }
}

Then in dbLogin you'd create an instance of dbConn class and access the string

myDbConn = new dbConn();
System.console.writeline(myDbConn.ConnStr); <-- Do whatever you want instead of printing it.


That's assuming both files are in the same assembly, if not you'd have to make it public.

Although, this is kind of overkill. Just like Markbnj said, create a resource for it in your project and store it there.
 
Jun 2, 2008
163
0
0
I tried it but I get a error.

dbConn.cs

public class dbConn
{
string connStr;

public dbConn()
{
connStr = (ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString);
}

internal string connStr
{
get { return this.connStr; }
}

}

I get this error "The type 'dbConn' already contains a definition for connStr"

Obviously because it's been established already, I guess I don't quite understand how this implements with my code.

Thanks

I will try and make a resource.
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
Your accessor is the same name (and case) as the variable it trying to pass. Typically, you can give them the same name but use different case to distinguish between them.

Hence, use this:

internal string ConnStr
{
get { return this.connStr; }
}

instead of:

internal string connStr
{
get { return this.connStr; }
}


 
Jun 2, 2008
163
0
0
Originally posted by: fishjie
I use a web.config file or an app.config to store my connection strings

So do I but you still have to call it in C# and I want one area to change that so I don't have to look for all the ConnectionStrings I made.
 

BuncoBaggins

Junior Member
Jul 4, 2008
6
0
0
The most important thing is to have very clean separation between your data layer and the layer that uses it. IOW if you've set up your interfaces correctly, your question really doesn't matter much in the grand scheme of things.
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Originally posted by: TheEarthWillShake
Originally posted by: fishjie
I use a web.config file or an app.config to store my connection strings

So do I but you still have to call it in C# and I want one area to change that so I don't have to look for all the ConnectionStrings I made.
I do the same as fishjie.

I'm not quite sure I understand your post here. Having it in the .config file is only having one area to change it.
 
Jun 2, 2008
163
0
0
I mean like this.

Say this is my web.config....

<connectionStrings>
<add name="OWCDEMOConnectionString" connectionString="Data Source=LAPPY\sqlexpress;Initial Catalog=OWCDEMO;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
</connectionStrings>

Now I need to call it from a page in C#

Page1.aspx
//Create a SQL connection, the parameters are located in the Web.Config file
String cn = ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString;

Say I need it in Page2.aspx
//Create a SQL connection, the parameters are located in the Web.Config file
String cn = ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString;


Now say I need to change the web.config file, well now I have to change page1 and page2 and the other pages as well that are connecting to the database through C#.

 

fishjie

Senior member
Apr 22, 2006
234
0
76
www.youtube.com
^ right, the config files let you store the connection string in one place. as long as you don't change the name of the connection string, i don't see what the problem is.