我试图根据数据库行的变化使缓存无效。我使用相同的数据库和表格对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或其他未设置权限的问题。 PLease帮助我解决这个问题。
答案 0 :(得分:1)
本文介绍了一些问题排查步骤The Mysterious Notification,并解释了一些有用的方法,可以帮助您了解某些内容被破坏的原因。您应该验证每件机器的工作原理:
sys.dm_qn_subscriptions
在不相关的说明中,您可以直接使用LINQ-to-SQL和查询通知:LinqToCache。
答案 1 :(得分:0)
最好的办法是:查看sql日志,如果发生任何Notifications问题,日志会显示!
错误日志位于 Program Files \ Microsoft SQL Server \ MSSQL.n \ MSSQL \ LOG \ ERRORLOG和ERRORLOG.n
在我的情况下,问题发生是因为数据库恢复,解决,日志显示我的问题,然后,我执行
use DatabaseName EXEC sp_changedbowner 'sa'