-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Varad_RS
Message 42 in Discussion
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/
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
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.
mailto:[EMAIL PROTECTED]