Varad's .NET Info
Reply
![]() |
|
From:
![]() Varad_RS
|
ASP.NET 2.0, Using Connection Strings from web.config -------------------------------------------------------------------------------
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>
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]" />
Regards, Varad
_________________________________________________________________ FREE pop-up blocking with the new MSN Toolbar get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
|
|
View other groups in this category.
![]() |
To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.
Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.
If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.
|
|
- Re: Varad's .NET Info Varad_RS
-