SqlCacheDependency
2011-03-27 00:08 乱世文章 阅读(166) 评论(0) 编辑 收藏 举报SqlCacheDependency
web页面代码片段:
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的代码片段:
DataTableCache代码
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)