不允许使用 WHERE 子句的 SqlDependency 查询。怎样修改才有效呢?
我使用以下查询设置了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ModifyDT是什么类型?
QN 的 ADO.Net 参考不完整,完整列表位于 的 SQL 参考中创建通知查询。后者还列出了以下内容:
另一个问题可能是查询中发生的从字符串到日期时间的转换,这可能被认为是不确定的(这是 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:
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.您显然已经有一些可以生成日期的东西了
因此,不要在 SQL 上创建 SqlDependency,而是使用 SqlCommand 对象,
然后创建对命令的依赖关系,
现在获取当前数据集并将其保存在内存中,以便在依赖关系触发后可以使用它进行比较
You apparently have something that generates the date already in
So rather than create a SqlDependency on the SQL, use a SqlCommand object
then just create your dependency on the command
now get the current dataset and stick it in memory so you can use it to compare against once the dependency fires
如果您还没有弄清楚这一点,那么由三部分组成的表名就是问题所在,请像这样尝试。
If you have not already figured this out, the 3 part table name that is the problem, try it like this.