Csharp/C#教程:检测SQL数据库更改分享


检测SQL数据库更改

考虑这个例子:

INSERT INTO [Table] (column1) SELECT value1 

如果我要在SSMS中执行此命令,关于ac#forms应用程序,为了识别此事件,我需要做些什么? 像发生此事件时显示MessageBox的应用程序一样简单。 我似乎无法解决这个问题或找到任何有用的数据。 我试图使用SqlDependency但没有运气。 如果那是我需要走的道路,那么任何人都可以帮助我更好地理解这个概念吗?

如果要检测更改而不仅仅是插入,则可以使用SQL依赖关系来实现此目的。 您是否阅读并尝试过链接中的示例?

这是一个很好的“教程/示例”,可以帮助您完成基础知识 。

这是对查询通知的一个很好的概述。

它基本上是“SqlDependency对象可以与SqlCommand关联,以便检测查询结果何时与最初检索的结果不同。”

您必须首先启用查询通知,然后按照创建通知查询

上述就是C#学习教程:检测SQL数据库更改分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 void Initialization() { // Create a dependency connection. SqlDependency.Start(connectionString, queueName); } void SomeMethod() { // Assume connection is an open SqlConnection. // Create a new SqlCommand object which directly references (no synonyms) the data you want to check for changes. using (SqlCommand command=new SqlCommand("SELECT value1 FROM [Table]", connection)) { // Create a dependency and associate it with the SqlCommand. SqlDependency dependency=new SqlDependency(command); // Maintain the refence in a class member. // Subscribe to the SqlDependency event. dependency.OnChange+=new OnChangeEventHandler(OnDependencyChange); // Execute the command. using (SqlDataReader reader = command.ExecuteReader()) { // Process the DataReader. } } } // Handler method void OnDependencyChange(object sender, SqlNotificationEventArgs e ) { // Handle the event (for example, invalidate this cache entry). } void Termination() { // Release the dependency. SqlDependency.Stop(connectionString, queueName); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1001634.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐