首页 新闻 会员 周边 捐助

SqlCacheDependency

0
悬赏园豆:50 [已关闭问题]

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();

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的时候,
我改变数据库中的数据时,界面上的数据能发生变化,通知失效机制能成功运行,我很困惑,这到底是什么原因?
小丁的主页 小丁 | 菜鸟二级 | 园豆:301
提问于:2010-01-19 11:57
< >
分享
其他回答(1)
0
这个有问题

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);
}

应该是你这边全局static有点问题,更改成静态单件构造函

你更改成试试:
 public static void AddCache(String key, DataTable dt,SqlCommand command) {       
SqlCacheDependency
_dependency = new SqlCacheDependency(command);
HttpContext.Current.Cache.Insert(key, dt, _dependency);
}


persialee | 园豆:3217 (老鸟四级) | 2010-01-19 15:47
这个我试过,好像也不行, 还有就是问题出在command上,在command执行以后,比如执行以下语句之后: command.ExecuteReader(); 或 SqlDataAdapter adapter = new SqlDataAdapter(command); 在把command赋给SqlCacheDependency时就会不起作用, 而在command执行之前赋给SqlCacheDependency就可以, 但我不太清楚这里边的运行机制
支持(0) 反对(0) 小丁 | 园豆:301 (菜鸟二级) | 2010-01-19 16:59
0

看看这里
http://www.dotnetcurry.com/ShowArticle.aspx?ID=263&AspxAutoDetectCookieSupport=1

的确需要在 cmd.ExecuteNonQuery();之前执行

  SqlCacheDependency dependency = new SqlCacheDependency(cmd);


邀月 | 园豆:25475 (高人七级) | 2010-02-01 08:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册