SqlCacheDependency
web頁面代碼片段:

String connectionString
=
"
Data Source=localhost;Initial Catalog=BalloonShop;Integrated Security=True
"
;
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();
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的代碼片段:

public
class
DataTableCache
{
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);
}
}
當我使用上面紅色的部分插入cache的時候,我改變數據庫中的數據時,界面上的數據卻不會變化,通知失效機制不能成功運行,
當我使用藍色的部分插入cache的時候, 我改變數據庫中的數據時,界面上的數據能發生變化,通知失效機制能成功運行,我很困惑,這到底是什么原因?
{
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);
}
}
當我使用上面紅色的部分插入cache的時候,我改變數據庫中的數據時,界面上的數據卻不會變化,通知失效機制不能成功運行,
當我使用藍色的部分插入cache的時候, 我改變數據庫中的數據時,界面上的數據能發生變化,通知失效機制能成功運行,我很困惑,這到底是什么原因?
?
所有回答(2)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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