使用 SqlCommand 作为缓存键时,SqlCacheDependency 不起作用
这段代码工作正常,每当数据库中的数据发生更改时,它就会使数据无效:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,根据此处,您不能使用
SELECT *
进行查询。以下更改应该可以解决您的问题:
The problem is, according to the rules outlined here, that you cannot use
SELECT *
for the query.The following change should solve your issue:
正如competent_tech指出的那样,用于构建
SqlCacheDependency
的查询有相当多的规则。根据这篇 MSDN 文章,最重要的是:
除了这些规则之外,执行用于构建
SqlCacheDependency
的SqlCommand
也很重要,以便启用查询通知:希望,这会有所帮助。
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:
Beside those rules it is important to execute the
SqlCommand
used to build theSqlCacheDependency
in order to enable the query notification:Hope, this helps.