咨詢電話:023-6276-4481
熱門文章
電 話:023-6276-4481
郵箱:broiling@qq.com
地址:重慶市南岸區(qū)亞太商谷6幢25-2
第一步:修改配置文件
<connectionStrings>
<add name="ConnectionString" connectionString="Server=(local);database=建立緩存的數(shù)據(jù)庫;uid=sa;pwd=123456" providerName="System.Data.SqlClient" />
</connectionStrings>
<!-- 定義緩存策略-->
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add connectionStringName="ConnectionString" name="建立緩存的數(shù)據(jù)庫"/>
</databases>
</sqlCacheDependency>
</caching>
第二步: 建立緩存
SqlCacheDependencyAdmin.EnableNotifications(數(shù)據(jù)庫連接字符串);
//連接到 SQL Server 數(shù)據(jù)庫并為 SqlCacheDependency 更改通知準備數(shù)據(jù)庫表
SqlCacheDependencyAdmin.EnableTableForNotifications(數(shù)據(jù)庫連接字符串, 表名,用來說明數(shù)據(jù)庫中哪些表更新時 重新建立緩存);
//定制緩存策略
SqlConnectionStringBuilder ConnectionStringBuilder = new SqlConnectionStringBuilder(數(shù)據(jù)庫連接字符串);
//建立依賴性
SqlCacheDependency CacheDependency = new SqlCacheDependency(數(shù)據(jù)庫名(必須與配置文件中的配置的名字一致),進行緩村的表名);
SqlDataAdapter Adapter = newSqlDataAdapter
Adapter.SelectCommand = "select * from 進行緩村的表名";
DataTable dt = new DataTable();
Adapter.Fill(dt);
if(HttpRuntime.Cache[應用程序緩存名]==null)
{
HttpRuntime.Cache.Add(應用程序緩存名, dt, CacheDependency, Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0), CacheItemPriority.High, null);
return dt;
}
else
{
return (DataTable)HttpRuntime.Cache[應用程序緩存名];
}