不允许使用 WHERE 子句的 SqlDependency 查询。怎样修改才有效呢?

发布于 2024-08-23 06:59:16 字数 764 浏览 12 评论 0原文

我使用以下查询设置了 SqlDependency:

string sql = "SELECT dbo.Case.CMRID, dbo.Case.SolutionID, dbo.Case.CreateDT, dbo.Case.ModifyDT "
+ "FROM dbo.Case "
+ "WHERE dbo.Case.ModifyDT > @LastExecutionDateTime";

执行此查询会导致 OnChanged 事件连续触发,类型为 Invalid 且来源为 Statement。经过进一步研究,我发现当您的查询破坏与索引视图规则相同的规则时会发生什么,因为这就是此通知机制的基础。

检查使用查询通知 (ADO.NET) 的特殊注意事项 我愿意没有看到我的声明违反了任何规则。

将语句修改为

string sql = "SELECT dbo.Case.CMRID, dbo.Case.SolutionID, dbo.Case.CreateDT, dbo.Case.ModifyDT "
+ "FROM dbo.Case";

“工作正常”。 OnChanged 事件仅在适当时触发,并且设置了正确的类型。

那么,如何才能只返回自上次执行语句以来具有修改日期的记录呢?

I have a SqlDependency set up using the following query:

string sql = "SELECT dbo.Case.CMRID, dbo.Case.SolutionID, dbo.Case.CreateDT, dbo.Case.ModifyDT "
+ "FROM dbo.Case "
+ "WHERE dbo.Case.ModifyDT > @LastExecutionDateTime";

Executing this query causes the OnChanged event to fire continuously with a type of Invalid and Source of Statement. Which upon further research I found is what happens when your query breaks rules that are the same as the rules for indexed views since that is what this notification mechanism is based on.

Checking the Special Considerations Using Query Notifications (ADO.NET) I do not see any rules I am breaking with this statement.

Modifying the statement to

string sql = "SELECT dbo.Case.CMRID, dbo.Case.SolutionID, dbo.Case.CreateDT, dbo.Case.ModifyDT "
+ "FROM dbo.Case";

Does work correctly. The OnChanged event only fires when appropriate, and has the correct type set.

So, how can I only return records with a modify date since my last execution of the statement?

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

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

发布评论

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

评论(3

怀中猫帐中妖 2024-08-30 06:59:17

ModifyDT是什么类型?

QN 的 ADO.Net 参考不完整,完整列表位于 的 SQL 参考中创建通知查询。后者还列出了以下内容:

该语句不能有比较
或基于 double/real 的表达式
数据类型。

另一个问题可能是查询中发生的从字符串到日期时间的转换,这可能被认为是不确定的(这是 ADO.Net 和 SQL 规范列出的标准)。尝试改用类型化参数: WHERE ModifyDT > @lastDateTime 并传入 DateTime 类型的参数。

What type is ModifyDT?

The ADO.Net reference for QN is incomplete, the complete list is in the SQL reference at Creating a Query for Notification. The latter also lists the following:

The statement must not have comparison
or expression based on double/real
data types.

Another problem may be the cast from string to datetime that occurs in your query, which may be considered non-deterministic (which is a criteria listed by both ADO.Net and SQL spec). Try using a typed parameter instead: WHERE ModifyDT > @lastDateTime and pass in a parameter of type DateTime.

伤感在游骋 2024-08-30 06:59:17

您显然已经有一些可以生成日期的东西了

最后执行日期时间

因此,不要在 SQL 上创建 SqlDependency,而是使用 SqlCommand 对象,

string sql = "SELECT CMRID, SolutionID, CreateDT, ModifyDT " + "FROM dbo.Case " + "WHERE ModifyDT > @lastExecutionDateTime"; 
//notice the parameter @lastExecutionDateTime, you cant use dates as a string, you also cant use something like CONVERT(datetime, '20040508'). You need a real date time object, hence the parameter

//You also only need to use the two part table ref (dbo.x) in the FROM clause, you dont need it on every field
//and while you didnt do it here, if anyone is interested a two part table ref in the form of dbo.[Case] would fail because the brackets will kill your dependency subscription

SqlCommand dependencyCommand= new SqlCommand(sql);
dependencyCommand.Parameters.Add(new SqlParameter("lastExecutionDateTime", SqlDbType.DateTime) {
        Value = LastExecutionDateTime 
    });

然后创建对命令的依赖关系,

//Create a dependency object and associate it with the SqlCommand.
SqlDependency dependency = new SqlDependency();
dependency.AddCommandDependency(dependencyCommand);
//Subscribe to the SqlDependency event.
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);

现在获取当前数据集并将其保存在内存中,以便在依赖关系触发后可以使用它进行比较

//get the most recent data
DataTable currentDependencyData = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter(dependencyCommand);
dataAdapter.Fill(currentDependencyData);

You apparently have something that generates the date already in

LastExecutionDateTime

So rather than create a SqlDependency on the SQL, use a SqlCommand object

string sql = "SELECT CMRID, SolutionID, CreateDT, ModifyDT " + "FROM dbo.Case " + "WHERE ModifyDT > @lastExecutionDateTime"; 
//notice the parameter @lastExecutionDateTime, you cant use dates as a string, you also cant use something like CONVERT(datetime, '20040508'). You need a real date time object, hence the parameter

//You also only need to use the two part table ref (dbo.x) in the FROM clause, you dont need it on every field
//and while you didnt do it here, if anyone is interested a two part table ref in the form of dbo.[Case] would fail because the brackets will kill your dependency subscription

SqlCommand dependencyCommand= new SqlCommand(sql);
dependencyCommand.Parameters.Add(new SqlParameter("lastExecutionDateTime", SqlDbType.DateTime) {
        Value = LastExecutionDateTime 
    });

then just create your dependency on the command

//Create a dependency object and associate it with the SqlCommand.
SqlDependency dependency = new SqlDependency();
dependency.AddCommandDependency(dependencyCommand);
//Subscribe to the SqlDependency event.
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);

now get the current dataset and stick it in memory so you can use it to compare against once the dependency fires

//get the most recent data
DataTable currentDependencyData = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter(dependencyCommand);
dataAdapter.Fill(currentDependencyData);
枕花眠 2024-08-30 06:59:17

如果您还没有弄清楚这一点,那么由三部分组成的表名就是问题所在,请像这样尝试。

"SELECT [CMRID], 
       [SolutionID], 
       [CreateDT], 
       [ModifyDT] 
FROM [dbo].[Case] 
WHERE [ModifyDT] > " + LastExecutionDateTime;

If you have not already figured this out, the 3 part table name that is the problem, try it like this.

"SELECT [CMRID], 
       [SolutionID], 
       [CreateDT], 
       [ModifyDT] 
FROM [dbo].[Case] 
WHERE [ModifyDT] > " + LastExecutionDateTime;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文