asp.net page SqlCacheDependency cache instance

  • 2021-01-18 06:23:43
  • OfStack

Caching technique is a very practical technique in asp.net programming, and it is also a common technique in large web programming. In this paper, the form of examples to illustrate this. The details are as follows:

asp.net page for SqlCacheDependency Sql cache:

Cahce/SqlCachePage.aspx page code is as follows:


<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SqlCachePage.aspx.cs"
  Inherits="Cahce_SqlCachePage" Title=" page Sql The cache " %>
<%@ 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 it is SqlServer2005 Is changed to SqlDependency="CommandNotification<br />
 Note the configuration in the configuration file 
</p>
<p>
<%=DateTime.Now %>
</p>
</asp:Content>

Sql cache for data source controls:

Cahce/SqlCachePage.aspx page code is as follows:


<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SqlCacheDataSourceControl.aspx.cs"
  Inherits="Cahce_SqlCacheDataSourceControl" Title=" The data source control Sql The cache " %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
DataSource Control sets the following properties: EnableCaching="True" SqlCacheDependency="VS2005_Test:sqlcache"
CacheDuration="Infinite"<br />
 If it is SqlServer2005 Is changed to SqlDependency="CommandNotification<br />
 Note the configuration in the 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>

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 If so, you only need the following Settings because SqlServer Support for notification based cache invalidation 
<sqlCacheDependency enabled="true" />
-->
</caching>
</system.web>

Note: For Sql Server 2005, the notification based cache invalidation is not set to aspnet_regsql, the property SqlDependency="CommandNotification" is set to SqlDependency. Before executing a SQL queries for the first time, must be somewhere in the application calls System. Data. SqlClient. SqlDependency. Start () method. This method should be placed in the Application_Start() event of the global.asax file.

I hope the caching technology described in this paper is helpful to asp.net program design.


Related articles: