SqlCacheDependency
web頁(yè)面代碼片段:

DataTable dt = (DataTable)HttpContext.Current.Cache[ " Customer_test " ];
if (dt == null )
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
String sqlStr = " SELECT dbo.Product.ProductID, Name, col_name FROM " +
" dbo.Product inner join dbo.ProductCategory on " +
" dbo.Product.ProductID = dbo.ProductCategory.ProductID inner join " +
" dbo.tb_category on dbo.ProductCategory.CategoryID = dbo.tb_category.col_id " ;
conn.Open();
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = sqlStr;
// DataTableCache.Dependency = new SqlCacheDependency(command);
SqlCacheDependency dependency = new SqlCacheDependency(command);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, " Customers " );
dt = ds.Tables[ 0 ];
// command.Connection.Close();
DataTableCache.AddCache( " Customer_test " , dt, command);//緩存通知失效 不起作用
DataTableCache.AddCache("Customer_test", dt, dependency );// 成功
// HttpContext.Current.Cache.Insert("Customer_test", dt, dependency);
}
}
gvwCustomers.DataSource = dt;
gvwCustomers.DataBind();
DataTableCahche的代碼片段:

{
private DataTableCache(){ }
private static SqlCacheDependency dependency = null ;
public static SqlCacheDependency Dependency {
get { return dependency; }
set { dependency = value; }
}
public static void AddCache(String key, DataTable dt,SqlCommand command) {
dependency = new SqlCacheDependency(command);
HttpContext.Current.Cache.Insert(key, dt, dependency);
}
public static void AddCache(String key, DataTable dt, SqlCacheDependency dependency) {
HttpContext.Current.Cache.Insert(key, dt, dependency);
}
public static void AddCache(String key, DataTable dt) {
HttpRuntime.Cache.Insert(key,dt, dependency);
}
}
當(dāng)我使用上面紅色的部分插入cache的時(shí)候,我改變數(shù)據(jù)庫(kù)中的數(shù)據(jù)時(shí),界面上的數(shù)據(jù)卻不會(huì)變化,通知失效機(jī)制不能成功運(yùn)行,
當(dāng)我使用藍(lán)色的部分插入cache的時(shí)候, 我改變數(shù)據(jù)庫(kù)中的數(shù)據(jù)時(shí),界面上的數(shù)據(jù)能發(fā)生變化,通知失效機(jī)制能成功運(yùn)行,我很困惑,這到底是什么原因?
?
這個(gè)有問(wèn)題
private static SqlCacheDependency dependency = null ;
public static SqlCacheDependency Dependency {
get { return dependency; }
set { dependency = value; }
}
public static void AddCache(String key, DataTable dt,SqlCommand command) {
dependency = new SqlCacheDependency(command);
HttpContext.Current.Cache.Insert(key, dt, dependency);
}
應(yīng)該是你這邊全局static有點(diǎn)問(wèn)題,更改成靜態(tài)單件構(gòu)造函
你更改成試試:
public static void AddCache(String key, DataTable dt,SqlCommand command) {
SqlCacheDependency _dependency = new SqlCacheDependency(command);
HttpContext.Current.Cache.Insert(key, dt, _dependency);
}
看看這里
http://www.dotnetcurry.com/ShowArticle.aspx?ID=263&AspxAutoDetectCookieSupport=1
的確需要在 cmd.ExecuteNonQuery();之前執(zhí)行
? SqlCacheDependency dependency = new SqlCacheDependency (cmd);
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

還有就是問(wèn)題出在command上,在command執(zhí)行以后,比如執(zhí)行以下語(yǔ)句之后:
command.ExecuteReader();
或
SqlDataAdapter adapter = new SqlDataAdapter(command);
在把command賦給SqlCacheDependency時(shí)就會(huì)不起作用,
而在command執(zhí)行之前賦給SqlCacheDependency就可以,
但我不太清楚這里邊的運(yùn)行機(jī)制