Caching technology is a very practical technique in asp.net programming, and it is a common technique in large web programming. This article explains this in the form of an instance. Specifically as follows:
SqlCacheDependency SQL cache for asp.net pages:
The cahce/sqlcachepage.aspx page code is as follows:
<%@ Page language= "C #" masterpagefile= "~/site.master" autoeventwireup= "true" codefile= "SqlCachePage.aspx.cs"
SQL cache for inherits= "cahce_sqlcachepage" title= page "%> <%@ OutputCache duration=" 999999 "sqldependency="
Vs2005_test:sqlcache "varybyparam=" None "%>
<asp:content id=" Content1 "contentplaceholderid=" ContentPlaceHolder1 "runat=" Server >
<p>
duration= "999999" sqldependency= "Vs2005_test:sqlcache" Varybyparam= "None" <br/>
if SqlServer2005 is changed to sqldependency= "
commandnotification<br/> Note Configuration file configuration
</p>
<p>
<%=datetime.now%>
</p>
</asp:Content>
SQL cache for data source control:
The cahce/sqlcachepage.aspx page code is as follows:
<%@ Page language= "C #" masterpagefile= "~/site.master" autoeventwireup= "true" codefile= " SqlCacheDataSourceControl.aspx.cs "inherits=" Cahce_sqlcachedatasourcecontrol "title=" SQL cache for data source control%> <asp:
Content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" > <p> The DataSource control sets the following properties: enablecaching= "True" sqlcachedependency= "Vs2005_test:sqlcache" cacheduration= "Infinite" <br /> If it is SqlServer2005, change it to sqldependency= "commandnotification<br/> Note configuration file </p> <p> <%= DateTime.Now%> </p> <p> <asp:sqldatasource id= "SqlDataSource1" runat= "Server" enablecaching= "True" Sqlcachedependency= "Vs2005_test:sqlcache" cacheduration= "Infinite" connectionstring= "<%$ ConnectionStrings: sqlConnectionString%> "selectcommand=" select * from [SQLCache] "></asp:SqlDataSource> <asp:gridview ID
= "GridView1" runat= "Server" datasourceid= "SqlDataSource1" allowsorting= "True" > </asp:GridView> </p> </asp:content&gT
Related Configuration in web.config:
<connectionStrings>
<add name= "sqlconnectionstring" connectionstring= "Data source=.\sqlexpress; attachdbfilename=| Datadirectory|\vs2005_test.mdf;integrated security=true; User instance=true "
providername=" System.Data.SqlClient/>
</connectionStrings>
< system.web>
<caching>
<sqlcachedependency enabled= "true" Polltime= "10000" >
< databases>
<add name= "vs2005_test" connectionstringname= sqlconnectionstring "/>"
</databases >
</sqlCacheDependency>
<!--If it is SqlServer2005, only the following settings are required because SQL Server supports notification based cache invalidation
< SqlCacheDependency enabled= "true"/>
-->
</caching>
</system.web>
Note: for SQL Server 2005来, caching based on notifications is invalidated, and the property sqldependency= "CommandNotification" is set without aspnet_regsql settings. Before you execute a SQL query for the first time, you must call the System.Data.SqlClient.SqlDependency.Start () method somewhere in the application. This method should be placed in the Application_Start () event of the Global.asax file.
I hope the caching techniques described in this article will help you with your asp.net programming.