100分求助一个SQLServer2005的查询通知的问题!!!
Global.asax:void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
string connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
System.Data.SqlClient.SqlDependency.Start(connString);
}
----------------------------------------------------------
public class Data
{
public Data()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public List<string> GetData()
{
List<string> data = HttpRuntime.Cache["Data"] as List<string>;
if (data == null)
{
data = new List<string>();
LoadAndCache(data);
}
return data;
}
public void ReLoadData(string key, object item, CacheItemRemovedReason reason)
{
List<string> data = new List<string>();
LoadAndCache(data);
}
private void LoadAndCache(List<string> data)
{
string connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string sql = "SELECT UserUid, [Name], UserID, [Password] FROM dbo.[User]";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dr.Read())
{
data.Add(dr.GetString(2));
}
}
//注册查询通知,方案一
SqlCacheDependency dep = new SqlCacheDependency(cmd);
HttpRuntime.Cache.Insert("Data", data, dep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(ReLoadData));
//方案二
//SqlDependency dep2 = new SqlDependency(cmd);
//HttpRuntime.Cache.Insert("Data", data);
//dep2.OnChange += new OnChangeEventHandler(dep2_OnChange);
conn.Close();
data = HttpRuntime.Cache["Data"] as List<string>;
}
void dep2_OnChange(object sender, SqlNotificationEventArgs e)
{
List<string> data = new List<string>();
LoadAndCache(data);
}
}
源码如上,VS2005SP1+VS2005,只有当VS的Development停止的时候,才会引发ReLoadData方法,而User表的数据变化却并不会引发ReLoadData方法!
--------------------编程问答-------------------- 帮忙顶 --------------------编程问答-------------------- 多谢帮顶! --------------------编程问答-------------------- hai 不会 帮d --------------------编程问答-------------------- 跪求高手!!! --------------------编程问答-------------------- 送分啊,没人要吗 --------------------编程问答-------------------- 自已顶一下,实在很急这个问题 --------------------编程问答-------------------- 我顶,呵呵,不会没有办法 --------------------编程问答-------------------- 那是必须的User表变了 当然不会引起查询 你应该换个方法 货思路 --------------------编程问答--------------------
xiaosonl
等 级:
结帖率:33.33%
补充:.NET技术 , ASP.NET