1.关于缓存依赖的测试。
if (!IsPostBack) { //System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, "Articles"); string key = "test"; DataTable data = (DataTable)HttpRuntime.Cache[key]; // Check if the data exists in the data cache if (data == null) { string strSql = "select top 5 * from Articles"; // If the data is not in the cache then fetch the data from the business logic tier data = HMBase.Data.SqlHelper.ExecuteSqlToTable(strSql); // Create a AggregateCacheDependency object from the factory AggregateCacheDependency cd = new AggregateCacheDependency(); cd.Add(new SqlCacheDependency("HMWeb", "Articles")); // Store the output in the data cache, and Add the necessary AggregateCacheDependency object HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration, CacheItemPriority.High, null); } GridView1.DataSource = data; GridView1.DataBind(); }
在此期间一共遇到了两个错误:
1:异常信息:无法在配置中找到“HMWeb”数据库。
在web.config文件里面配置;
节点:system.web节点里面再配置节点:
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add name="HMWeb" connectionStringName="ConnectionString" pollTime="10000"/>
</databases>
</sqlCacheDependency>
</caching>
异常2:异常信息:没有为 SQL 缓存通知启用数据库“HMWeb”。
那是因为没有注册sql依赖表(也许是生成一些触发器,存储过程之类的东西)
注册方法如下:
C:\Program Files\Microsoft Visual Studio 9.0\VC>aspnet_regsql.exe -S .\SQL2005 -U sa -P sa-ed -d HMWeb -et -t Articles
-s代表server -u代表UserName -p代表password -d代表database数据库 -t代表要对缓存依赖项启用的表名