Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to save the connection string in a class library pro
Message
From
15/10/2007 18:47:09
 
 
To
15/10/2007 14:56:43
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
OS:
Vista
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01261098
Message ID:
01261150
Views:
11
Sergio,

You don't actually want to save a hard-coded Connection String, do you? I wouldn't think so, it should be stored in a web.config or an app.config. What we've done is have a class like this in our DataAccess namespace:
public class MyConnection
{
	#region Declaration
	public string ConnectionString = "";
	#endregion

	#region Methods
	public IDbConnection GetConnection()
	{
		return this.GetConnection("");
	}
	public IDbConnection GetConnection(string tConnectionString)
	{
		string cConnection = tConnectionString;
		
		if(tConnectionString == "")
			cConnection = ConfigurationSettings.AppSettings["ConnectionString"];
		
		SqlConnection conn = new SqlConnection(cConnection);
		this.ConnectionString = cConnection;
		return conn;
	}
	#endregion
}
The ConfigurationSettings in the above code is from 1.1. It's slightly different in 2.0 (don't recall it off the top of my head), but that's mainly because the way the settings are written in the config files for 2.0 are slightly different. You can do it either way. Look it up in the docs. I'll try to help if you have questions.

[UPDATE]
I found a good link on the subject on The Code Project, which explains the difference between 1.1 and 2.0 syntax
:
http://www.codeproject.com/cs/database/Connection_Strings.asp

~~Bonnie





>I do have a Class library project and I would like to know what is the best way to save my connection string. I do not want display the connection string on all my clasess.
>
>Thanks,
>Sergio
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform