In ASP.NET 2.0, database connection strings are referenced by name using a new declarative expression syntax that resolves to a connection string value at runtime. The connection string itself is stored in the Web.config file under the <connectionStrings> configuration section so that it is easy to maintain in a single location for all pages in the application.
The sample program code is as follows:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Pubs" connectionString="Server=localhost;
Integrated Security=True;Database=pubs;Persist Security Info=True"
providerName="System.Data.SqlClient" />
<add name="Northwind" connectionString="Server=localhost;
Integrated Security=True;Database=Northwind;Persist Security Info=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<pages styleSheetTheme="Default"/>
</system.web>
</configuration>
Program code description: In the program code of the above example, we set two database connection strings under the <connectionStrings> configuration node in the Web.Config file, pointing to the two sample databases pubs and Northwind respectively. Note that data source controls, such as the SqlDataSource control, were introduced in 2.0. We can set the ConnectionString property of the SqlDataSource control to the expression <%$ ConnectionStrings:Pubs %>, which is parsed by the ASP.NET analyzer at runtime. is the connection string. You can also specify an expression for the ProviderName property of the SqlDataSource, such as <%$ ConnectionStrings:Pubs.ProviderName %>. Its specific usage and new features will be introduced in detail in subsequent chapters. Now you have a basic understanding.
Of course, we can also read the database connection string directly from the configuration file in the following way. First we need to reference the using System.Web.Configuration namespace, which contains the classes used to set up ASP.NET configuration.
string connectionString =ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
Program code description: In the program code of the above example, we can use ConnectionStrings["Northwind"] to read the corresponding Northwind string. In the same way, you can use ConnectionStrings["Pubs"] to read the corresponding Pubs string.
http://www.cnblogs.com/interboy/archive/2006/08/21/482665.html