Using ConnectionStrings Section in ASP.NET 2.0

ASP.NET 2.0 introduces a new section to the web.config file called ConnectionStrings. Some previous projects in ASP.NET 1.1 created this section so you may be familiar with it. This section allows users to add connection strings in their web.config for different data sources. An example of one using SqlExpress 2005 is shown below:

    <connectionStrings>
        <add name="MainConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\YourDB.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>


When referencing this connection string in a built-in DataSource control, you can choose the connection from within the control's wizard or manually modify the ConnectionString and ProviderName properties of the DataSource control. An example is shown below:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        SelectCommand="select * from [Users]"
        ConnectionString="<%$ ConnectionStrings:MainConnectionString %>"
        ProviderName="<%$ ConnectionStrings:MainConnectionString.ProviderName %>">
    </asp:SqlDataSource>


You can also access the connection string in a code file in the App_Code folder by using the ConfigurationManager class like this:

    Dim conn as SqlConnection
    conn = New SqlConnection(ConfigurationManager.ConnectionStrings("MainConnectionString").ConnectionString)


This makes accessing your connection strings and managing them much easier.
Published Thursday, May 12, 2005 3:14 PM by Jason N. Gaylord

Comments

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, May 12, 2005 3:43 PM by Hannes Preishuber
In B2 you should WebConfigurationManager

# re: Using ConnectionStrings Section in ASP.NET 2.0

Monday, May 16, 2005 9:38 PM by YeYan
RePost:
http://www.yeyan.cn/Programming/ConnectionStringsNET.aspx

# Using ConnectionStrings in ASP.NET 2.0

Monday, May 23, 2005 12:23 PM by TrackBack
TrackBack From:http://www.cnblogs.com/na57/archive/2005/05/24/161098.html

# re: Using ConnectionStrings Section in ASP.NET 2.0

Wednesday, October 18, 2006 2:06 AM by Cory Foy

It actually should be

ConfigurationManager.ConnectionStrings["blah"]

Or, in other words, the ConnectionStrings should be brackets instead of parens.

Good post though!

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, April 05, 2007 4:43 PM by Joe Dirt

Not in VB, assmonkey

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, May 24, 2007 3:15 PM by Guest

Please stop posting broken code.

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, June 21, 2007 10:29 AM by pinko

it is C# code:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnectionString"].ToString());

# re: Using ConnectionStrings Section in ASP.NET 2.0

Tuesday, July 24, 2007 11:15 AM by Stephen Lowe Watson

All fine as long as you always use the same provider. But you should be able to say

IDbConnection Conn = ConfigurationManager.ConnectionStrings.NewConnection("blah blah");

and get whatever connection type is specified in ProviderName

# re: Using ConnectionStrings Section in ASP.NET 2.0

Sunday, August 05, 2007 1:23 AM by Tsuruda

buy-tramadol--online.info/.../tramadol-sizes-and-shapes.php

# re: Using ConnectionStrings Section in ASP.NET 2.0

Saturday, September 22, 2007 8:13 AM by SD

Is it  possible in separate class Library project to access following Property ? System.Configuration.ConfigurationManager.ConnectionStrings []

I tried for this but it doesn't access above property in class library project but in ASPx page I am getting above property.

So I used traditional method in class library project as

System.Configuration.ConfigurationSettings.AppSettings["DbChangeControlCon"]...

# re: Using ConnectionStrings Section in ASP.NET 2.0

Saturday, September 22, 2007 8:35 AM by SD

Is it  possible in separate class Library project to access following Property ? System.Configuration.ConfigurationManager.ConnectionStrings []

I tried for this but it doesn't access above property in class library project but in ASPx page I am getting above property.

So I used traditional method in class library project as

System.Configuration.ConfigurationSettings.AppSettings["DbChangeControlCon"]...

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, November 29, 2007 6:24 PM by PRESTON,Play the race card

Play the race card

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, November 30, 2007 11:22 AM by MICKEY,We are a grandmother

We are a grandmother

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, February 01, 2008 12:33 AM by mehregan

Thanks

# Lexapro.

Monday, July 14, 2008 3:18 PM by Lexapro versus zoloft.

What is lexapro. Lexapro.

# Buspar and weight gain.

Thursday, July 31, 2008 1:32 AM by Buspar.

Buspar online cheaper. Buspar. Bristol-meyer squibb buspar. Buspar alcohol. Buspar and modafinil together.

Leave a Comment

(required) 
(required) 
(optional)
(required)