Press enter to see results or esc to cancel.

How To Get Connection Strings From web.config

In c# it’s quite easy to get a connection string from web.config at run-time without having to manually parse the xml with the aid of the System.Configuration library.

First you need a reference to the System.Configuration library – you add the reference via the “Add Reference” feature of your project and you then include the reference via the using directive at the top of your class file as follows:

using System.Configuration;

(Note: in .NET 4.0 the reference is added by default).

Once you have included the required reference, you can now access your connection string as follows (for the purposes of this example, the connection string is named MyConnectionString:

string myConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Comments

Leave a Comment