Friday, July 07, 2006

Get a connection string from web.config

I recently needed to get a connection string from my web.config file and had some trouble finding a definitive answer on how to do it when searching the web. Most of the info that I did trun up was for .NET 1 and I needed a solution for ASP.NET 2.0. I was finally able to get it working with some help and here is what needs to be done.
//Get the connection string
ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["myConnection"];
//Create the connection
SqlConnection connection = new SqlConnection(connectionString.ConnectionString);
Now that you have the connection you can use it however you wish.

In order for this to work I had to add the following using statement and refrence.
using System.Configuration;

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home