asp.net page SqlCacheDependency Cache instance _ Practical tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.