Scott Forsyth's Blog

Postings on IIS, ASP.NET, SQL Server, Webfarms and general system admin.

Using connection strings from web.config in ASP.NET v2.0

ASP.NET v2.0 has a couple new ways to reference connection strings stored in the web.config or machine.config file.

A typical web.config file in v2.0 could have the following section which is placed directly under the root <configuration> section.

<connectionStrings>
    <
remove name="LocalSqlServer"
/>
    <
add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"
/>
    <add name="MainConnStr" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|main.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

connectionStrings>
    <
remove name="LocalSqlServer"
/>
    <
add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"
/>
    <add name="MainConnStr" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|main.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

You can reference this directly from code using:

[C#]
string connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;

[VB]
Dim connStr As String = ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString

Note that the namespace for this is System.Configuration so for a console application the full namespace is required.


Or you can reference this declaratively within the ConnectionString property of a SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
  ConnectionString="<%$ ConnectionStrings:MainConnStr %>"
  SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
/>

Posted: Aug 26 2005, 11:18 AM by OWScott | with 16 comment(s) |
Filed under:

Comments

Munjal said:

Hello,

The connection strings you are discussing about actually connects to MSSQL 2005. I am using similar connection string for my application:

<connectionStrings>

<add name="Personal" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Personal.mdf" providerName="System.Data.SqlClient"/>

<remove name="LocalSqlServer"/>

<add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf"/>

</connectionStrings>

But unfortunately, my server doesn't support 2005 and asks me to use 2000. Can you help me out with changes I should be making in order to make it work with 2000 as well please?

I would be waiting for your replay. You can email me at: munjalpatel@ieee.org

Thanks :)

# December 7, 2006 11:53 PM

vozindependiente said:

That's not the first time this blog helps me. If you had had adsense you could have some cents in your account.

# January 8, 2007 4:22 PM

dofstudio2007 said:

,  ,  ,  ,  ,, , , www.dofstudio.ru,  ,  ,  , ,  ,   ,  ,  , ,www.dofstudio.ru, , ,  ,  , ,  , , , , ,  , , , , , ,  ,  ,  , ,www.dofstudio.ru,  ,  ,  ,  

[b][url=]http://www.dofstudio.ru[/url][/b]

# January 16, 2007 5:15 PM

Josh Stodola said:

What if my database is stored in the directory above App_Data?

Don't ask me why, it really isnt, but I would like to know.

# February 12, 2007 3:06 PM

Daranee said:

I've created a datagrid on a web page using vb.net. The web config file that is configured in visual studio works when I run the page works locally, but when I run the web page from our server I get a configuration error. It seems to be looking at a web.config file in another location of our server than the web site folder.

If I change the web config file to:

<configuration>

 <appSettings>

 </appSettings>

 <connectionStrings>

   <add name="ConnectionString" connectionString="data source=IP,Port; initial catalog=db; user id=username; password=password;"/>

 </connectionStrings>

 <system.web>

   <customErrors mode="Off"/>

   <compilation debug="true"/>

 </system.web>

</configuration>

It works fine. I would like to have the flexibility to refer to the connection string in the code, and I'm also curious what is going on.

FYI, I'm a beginner. Hope I provided enough info.

# February 15, 2007 4:20 PM

seva2008 said:

,  ,  ,

     http://web-profi.biz  

# March 21, 2007 4:28 PM

pechation said:

:             50    6000 .

      90 000   .    [url=http://www.pechation.ru][/url]   : 8-921-964-41-98

# April 8, 2007 10:34 AM

Anyutka-1985 said:

.  ,   ,     ,   ,   Wildhog,

 .      ,    ,  ,

      " "[url=saylormooyen.justfree.com].[/url]

,  .

[url=saylormooyen.justfree.com][img]saylormooyen.justfree.com/tm.jpg[/img][/url]

# May 30, 2007 5:52 AM

Papayib said:

<a href= dreamlessfennel.hotbox.ru >elmos lounge</a> <a href= http://middlebrowp.hotbox.ru >remax</a> <a href= http://brainless.pochtamt.ru >kountry mile</a> <a href= http://metalsl.front.ru >wkyc.co</a> <a href= http://bakhsh.land.ru >russian woman loves sex</a> <a href= appallinglyracecour.front.ru >florida department of finacial services</a> <a href= http://amountbackgamm.land.ru >weightwatcher</a> <a href= http://ncgaspringe.front.ru >volleyball set</a> <a href= http://opener.hotbox.ru >seton hall u</a> <a href= depletorypuerility.land.ru >bangbus</a>

# June 9, 2007 9:08 PM

arun said:

i want to know the same connection string when using a access database file and should have the path from the localhost

# June 12, 2007 1:25 AM

eqigacef said:

<a href= " chinesefreewebs.com/.../index.htm ">outer space to find another race lyrics</a>

<a href= " chinesefreewebs.com/.../index.htm ">american town network</a>

<a href= " chinesefreewebs.com/.../index.htm ">megan beebe</a>

<a href= " chinesefreewebs.com/.../index.htm ">Fiebre Fascinado Sidonie</a>

<a href= " chinesefreewebs.com/.../index.htm ">wild flowers lyrics</a>

<a href= " chinesefreewebs.com/.../index.htm ">nothing is easy jethro tull</a>

<a href= " chinesefreewebs.com/.../index.htm ">salut techno</a>

<a href= " chinesefreewebs.com/.../index.htm ">Ottos Way Incredible Sound of Drum and Bass Various Artists</a>

<a href= " chinesefreewebs.com/.../index.htm ">out gunned</a>

<a href= " chinesefreewebs.com/.../index.htm ">the circle of life lion king lyrics</a>

<a href= " chinesefreewebs.com/.../index.htm ">caverns of exile map</a>

<a href= " chinesefreewebs.com/.../index.htm ">arsenic treated lumber</a>

<a href= " chinesefreewebs.com/.../index.htm ">the forgotten premiere</a>

<a href= " chinesefreewebs.com/.../index.htm ">mclarin slr</a>

<a href= " chinesefreewebs.com/.../index.htm ">shelley percy bysshe</a>

<a href= " chinesefreewebs.com/.../index.htm ">java runetime download</a>

<a href= " chinesefreewebs.com/.../index.htm ">history of science</a>

<a href= " chinesefreewebs.com/.../index.htm ">monthly parking ottawa downtown</a>

<a href= " chinesefreewebs.com/.../index.htm ">mini usb harddrives</a>

<a href= " chinesefreewebs.com/.../index.htm ">wynndum</a>

<a href= " chinesefreewebs.com/.../index.htm ">english to hindi dictonary</a>

<a href= " chinesefreewebs.com/.../index.htm ">how to use drive snapshot</a>

<a href= " chinesefreewebs.com/.../index.htm ">disney store in manhattan</a>

# June 17, 2007 1:38 AM

Daniel said:

Dear Ms/Sir

Hi.

We have a VPS software with its modules. I installed the VPS and configured it, it works properly without any problem.

But I couldn't install and configure the web based modules and features. I know that they need to be expert on ASP and IIS.

I would like to kindly ask you to help me in installation, configuration and setting up the web based features and modules please.

It is very important and urgent for us.

If there is any documents for the configuration and setting up the VPS and its Modules, May I kindly ask you to send them to me please.

I am very interested to learning the installation and configuration of VPS and its modules. Could you Please help me.

Thanks a lot for your attention.

Regards,

Daniel.

# July 4, 2007 6:28 PM

ctl00$main$ctl10$ctl02$ctl02$ctl02$tbname said:

boob and implants <a href= http://boobimplants.vdforum.ru >after implants boob</a> [url=http://boobimplants.vdforum.ru]after implants boob[/url]

# July 21, 2007 7:06 AM

Andy said:

I want to know how to connect sql express database using asp.net 2.0 by passing connection string in <appSettings> section of web.config file.

Thanks & Regards,

Andy

# August 5, 2007 11:13 AM