緩存技術是asp.net程序設計中非常實用的技術,也是大型web程序設計中比較常見的技術。本文就以實例形式對此加以說明。具體如下:
asp.net頁面的SqlCacheDependency Sql緩存:
Cahce/SqlCachePage.aspx頁面代碼如下:
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SqlCachePage.aspx.cs" Inherits="Cahce_SqlCachePage" style="margin: 3px auto 0px; padding: 0px 0px 0px 5px; font-family: 'Courier New', Courier, monospace; border-left-color: rgb(108, 226, 108); border-left-width: 3px; border-left-style: solid; width: 640px; clear: both; line-height: 20px;"><%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SqlCacheDataSourceControl.aspx.cs" Inherits="Cahce_SqlCacheDataSourceControl" style="margin: 3px auto 0px; padding: 0px 0px 0px 5px; font-family: 'Courier New', Courier, monospace; border-left-color: rgb(108, 226, 108); border-left-width: 3px; border-left-style: solid; width: 640px; clear: both; line-height: 20px;"> <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><!-- 如果是SqlServer2005的話,則只需如下設置,因為SqlServer支持基于通知的緩存失效<sqlCacheDependency enabled="true" />--></caching></system.web>
注意:對于Sql Server 2005來說,基于通知的緩存失效,不用aspnet_regsql設置,要設置屬性SqlDependency="CommandNotification"。在首次執行某 SQL 查詢之前,必須在應用程序某處調用 System.Data.SqlClient.SqlDependency.Start() 方法。此方法應放在 global.asax 文件的 Application_Start() 事件中。
希望本文所述的緩存技術對大家的asp.net程序設計有所幫助。
新聞熱點
疑難解答
圖片精選