Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  105 Posts | 1 Stories | 503 Comments | 65 Trackbacks

News



Archives

ASP.NET Ventures

We can very easily access connection strings (defined in Web.config) inside other class library projects included in the same solution as the web project simply using the System.Configuration namespace.

Here is the code for ASP.NET 2.0:

System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

Note: Don't forget to include a reference to System.Configuration dll in your class library project using Add Reference in VS.

In ASP.NET 1.x:

System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; 

Check out this extremely useful website for connection strings:

http://www.connectionstrings.com

Hope this helps!

posted on Saturday, February 24, 2007 11:22 PM

Feedback

# re: How to: Access connection strings in class libraries 2/25/2007 8:10 PM Chris Carter
Or you can future proof your class library by pulling out the dependency on AppSettings or ConnectionStrings and having your library require the connection string either in method signatures or constructors.

# re: How to: Access connection strings in class libraries 5/12/2007 11:34 AM Deepak
I tend to use a very similar approach mentioned by Chris, since the Connection string for a website should be configurable via the web.config in the presentation tier, we need to remove any dependencies from the class library. Instead we "Inject" this behavior in the constructor of the class library when invoked from the upper (UI) layer.

Thanks,
Deepak.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: