无法使依赖于 sql 查询的缓存失效

发布于 2024-10-05 19:18:08 字数 1446 浏览 3 评论 0原文

我试图根据数据库行的更改使缓存无效。我在使用相同的数据库和表使用 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 或其他权限。请帮助我解决这个问题。

I am trying to invalidate the cache based on the change in the rows of the database. I have achieved success while doing the same thing with linq with the same database and table but Iam not able to do it in the basic way:

Here is the code that I have written, please help:

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;

    }

I am using Sql Server 2005 and using the fully qualified queries that can work with command notification.

My other code in Linq is perfectly working with the same database and table, so there is no point about the Service Broker or other permissions not set. PLease help me to get rid of this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-10-12 19:18:08

本文提供了一些故障排除步骤神秘通知,并附有说明事物如何运作有助于理解事物为何出现故障。您应该验证每个部分是否正常工作:

在不相关的注释中,您可以直接使用 LINQ-to-SQL 和查询通知: LinqToCache

This article has some troubleshooting steps The Mysterious Notification, along with an explanation how things work that helps where trying to understand why something is broken. Your should validate that each piece of the machinery works:

  • check the a subscription notification gets created in sys.dm_qn_subscriptions
  • check that a notification gets fired
  • check that the notification gets delivered (you can safely ignore everything about routing and remote delivery in this link, though, since SqlDependency is always local)

On an unrelated note, you can use LINQ-to-SQL and Query Notifications directly: LinqToCache.

药祭#氼 2024-10-12 19:18:08

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

错误日志位于
Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG 和 ERRORLOG.n

在我的例子中,问题是由于数据库恢复而发生的,为了解决,日志向我显示了问题,然后,我执行

use DatabaseName EXEC sp_changedbowner 'sa'

The best thing to do is: Take a look on the sql log, if any problem with Notifications is happening, log will show!

the error log is located at
Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG and ERRORLOG.n

In my case, The problem occurred because of database restoration, to solve, log showed me the problem, then, I execute

use DatabaseName EXEC sp_changedbowner 'sa'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文