[英]Unable to invalidate the cache dependent on sql query
我試圖根據數據庫行中的更改使緩存無效。 我在使用具有相同數據庫和表的linq進行相同操作時取得了成功,但是我無法以基本方式做到這一點:
這是我編寫的代碼,請幫助:
public DataSet GetData(string sqlCmd)
{
string connectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
string cacheKey = "test123";
DataSet ds = (DataSet)HttpRuntime.Cache.Get(cacheKey);
if (ds != null)
{
return ds;
}
SqlCacheDependency sqldep = null;
SqlConnection conn = new SqlConnection(connectionStr);
SqlCommand cmd = new SqlCommand(sqlCmd, conn);
cmd.Notification = null;
cmd.NotificationAutoEnlist = true;
SqlDataAdapter sqlAd = new SqlDataAdapter(cmd);
ds = new DataSet();
sqlAd.Fill(ds);
System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connectionStr);
string NotificationTable = "TblMetaData";
if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connectionStr).Contains(NotificationTable))
System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connectionStr, NotificationTable);
sqldep = new SqlCacheDependency(cmd);
HttpRuntime.Cache.Insert(cacheKey, ds, sqldep);
return ds;
}
我正在使用Sql Server 2005,並使用可以與命令通知一起使用的標准查詢。
我在Linq中的其他代碼可以完美地使用相同的數據庫和表,因此關於Service Broker或未設置其他權限毫無意義。 請幫助我擺脫這個問題。
本文提供了一些疑難解答步驟The Mysterious Notification ,以及有關工作原理的解釋,可幫助您了解為什么損壞了某些內容。 您應該驗證每台機器是否正常工作:
sys.dm_qn_subscriptions
創建的訂閱通知 無關緊要的是,您可以直接使用LINQ-to-SQL和查詢通知: LinqToCache 。
最好的辦法是:查看sql日志,如果Notifications發生任何問題,將顯示日志!
錯誤日志位於Program Files \\ Microsoft SQL Server \\ MSSQL.n \\ MSSQL \\ LOG \\ ERRORLOG和ERRORLOG.n
以我為例,該問題是由於數據庫還原而發生的,要解決,日志顯示了問題,然后執行
use DatabaseName EXEC sp_changedbowner 'sa'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.