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:
1
2
3
4
5
<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:
1
2
3
4
5
<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:
1
2
Dim conn as SqlConnection
conn = New SqlConnection(ConfigurationManager.ConnectionStrings("MainConnectionString").ConnectionString)
This makes accessing your connection strings and managing them much easier.