在Web.config设置
<?xml version="1.0"?> <configuration> <connectionStrings> <!-- This connection is inherited from the ASP.NET Quickstart Web.config file Uncomment this section to edit the sample locally <add name="Pubs" connectionString="Server=(local)/SQLExpress;Integrated Security=True;Database=pubs;Persist Security Info=True" providerName="System.Data.SqlClient" /> <add name="Northwind" connectionString="Server=(local)/SQLExpress;Integrated Security=True;Database=Northwind;Persist Security Info=True" providerName="System.Data.SqlClient" /> <add name="Contacts" connectionString="Server=(local)/SQLExpress;Integrated Security=True;Database=Contacts;Persist Security Info=True" providerName="System.Data.SqlClient" /> --> <add name="NorthwindOLEDB" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb;" providerName="System.Data.OleDb" /> <add name="ContactsDatabase" connectionString="Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=true;" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <pages styleSheetTheme="Default"/> <caching> <sqlCacheDependency enabled="true" pollTime="1000"> <databases> <add name="Pubs" connectionStringName="Pubs"/> </databases> </sqlCacheDependency> </caching> </system.web> </configuration>
在代码中访问:
ConnectionString="<%$ ConnectionStrings:Pubs %>" />