ASP.NET2.0中通过web.config设置数据库连接串
Web.config代码:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Pubs" connectionString="Server=localhost;uid=sa;pwd=123456; database=pubs;" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true"/>
<caching>
<sqlCacheDependency enabled="true" pollTime="1000">
<databases>
<add name="Pubs" connectionStringName="Pubs"/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
Default.aspx代码:
<%@ Page Language="C#" %>
<html>
<head runat="server">
<title>GridView Bound to SqlDataSource</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]"
ConnectionString="<%$ ConnectionStrings:Pubs %>" />
</form>
</body>
</html>