使用 SqlCommand 作为缓存键时,SqlCacheDependency 不起作用

发布于 2024-12-25 20:49:08 字数 1013 浏览 2 评论 0原文

这段代码工作正常,每当数据库中的数据发生更改时,它就会使数据无效:

    AggregateCacheDependency aggDep = new AggregateCacheDependency();

                System.Data.SqlClient.SqlCommand ocom = new System.Data.SqlClient.SqlCommand();
                SqlCacheDependency SqlDep = new SqlCacheDependency("DBNAMEINCONFIG", "Products");    
                aggDep.Add(SqlDep);

不过,我无法对整个“产品”表进行直接无效,我需要能够使表上的选择无效。我遇到的问题是,当数据更改时,以下代码永远不会使缓存无效:

AggregateCacheDependency aggDep = new AggregateCacheDependency();

            System.Data.SqlClient.SqlCommand ocom = new System.Data.SqlClient.SqlCommand();
            ocom.CommandText = "SELECT ID,ClinicID,Price,Enabled FROM dbo.Products WHERE ClinicID = 1";
            ocom.Connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["DBSTRING"].ToString());
            SqlCacheDependency SqlDep = new SqlCacheDependency(ocom);

            aggDep.Add(SqlDep);

我尝试包含分析此数据所需的所有信息,但请告诉我是否应该包含更多信息!

This code works fine, it invalidates the data whenever it is changed in the database:

    AggregateCacheDependency aggDep = new AggregateCacheDependency();

                System.Data.SqlClient.SqlCommand ocom = new System.Data.SqlClient.SqlCommand();
                SqlCacheDependency SqlDep = new SqlCacheDependency("DBNAMEINCONFIG", "Products");    
                aggDep.Add(SqlDep);

I cannot have a straight invalidate on the entire "Products" table though, I need to be able to invalidate a selection on the table. The problem I'm having is the following code does not ever invalidate the cache when the data is changed:

AggregateCacheDependency aggDep = new AggregateCacheDependency();

            System.Data.SqlClient.SqlCommand ocom = new System.Data.SqlClient.SqlCommand();
            ocom.CommandText = "SELECT ID,ClinicID,Price,Enabled FROM dbo.Products WHERE ClinicID = 1";
            ocom.Connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["DBSTRING"].ToString());
            SqlCacheDependency SqlDep = new SqlCacheDependency(ocom);

            aggDep.Add(SqlDep);

I tried to include all the information necessary to analyze this, but please let me know if I should include more!

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

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

发布评论

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

评论(2

假面具 2025-01-01 20:49:08

问题是,根据此处,您不能使用 SELECT * 进行查询。

以下更改应该可以解决您的问题:

ocom.CommandText = "SELECT ClinicID FROM Products WHERE ClinicID = 1";

The problem is, according to the rules outlined here, that you cannot use SELECT * for the query.

The following change should solve your issue:

ocom.CommandText = "SELECT ClinicID FROM Products WHERE ClinicID = 1";
拿命拼未来 2025-01-01 20:49:08

正如competent_tech指出的那样,用于构建SqlCacheDependency的查询有相当多的规则。
根据这篇 MSDN 文章,最重要的是:

  1. 不要使用 SELECT *或 table_name.* 在您的查询中。
  2. 您必须使用完全限定的表名称(例如 dbo.Products)。

除了这些规则之外,执行用于构建 SqlCacheDependencySqlCommand 也很重要,以便启用查询通知:

using (SqlDataReader reader = ocom.ExecuteReader())
{
  while (reader.Read())
  {           
  }
} 

SqlCacheDependency SqlDep = new SqlCacheDependency(ocom);

aggDep.Add(SqlDep);

希望,这会有所帮助。

As competent_tech pointed out there are quite a few rules for the queries used to build SqlCacheDependency.
According to this MSDN article the most important are:

  1. Do not use SELECT * or table_name.* in your queries.
  2. You must use fully qualified table names (e.g. dbo.Products).

Beside those rules it is important to execute the SqlCommand used to build the SqlCacheDependency in order to enable the query notification:

using (SqlDataReader reader = ocom.ExecuteReader())
{
  while (reader.Read())
  {           
  }
} 

SqlCacheDependency SqlDep = new SqlCacheDependency(ocom);

aggDep.Add(SqlDep);

Hope, this helps.

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