我试图根据数据库行的更改使缓存无效。我在使用相同数据库和表格的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中的其他代码与同一数据库和表格完美合作,因此服务代理或未设置的其他权限没有意义。请帮助我摆脱这个问题。

有帮助吗?

解决方案

本文有一些故障排除步骤 神秘的通知, ,除了解释事物如何工作,在试图理解为什么破裂的地方有帮助。您应该验证机械的每一部分都可以使用:

  • 检查在 sys.dm_qn_subscriptions
  • 检查通知是否获取 开火
  • 检查通知是否获取 发表 (但是,您可以安全地忽略有关此链接中有关路由和远程交付的所有内容,因为SQLDEpendenty始终是局部

在不相关的情况下,您可以直接使用LINQ-TO-SQL并查询通知: linqtocache.

其他提示

最好的办法是:查看SQL日志,如果发生通知的任何问题,日志将显示!

错误日志位于Program Files Microsoft SQL Server MSSQL.N MSSQL MSSQL log errorlog和errorlog.n

就我而言,问题是由于数据库恢复而发生的

use DatabaseName EXEC sp_changedbowner 'sa'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top