How to monitor SQL Server table changes by using c#?(如何使用 c# 监控 SQL Server 表更改?)
问题描述
我有多个应用程序访问同一个数据库,如果其中一个应用程序更改某个表中的任何内容(更新、插入),我需要得到通知.
I have more than one application accessing the same DB and I need to get notified if one of these apps change anything (update, insert) in a certain table.
数据库和应用不在同一台服务器上.
Database and apps are not in the same server.
推荐答案
您可以使用 SqlDependency 类
.它的预期用途主要用于 ASP.NET 页面(客户端通知数量较少).
You can use the SqlDependency Class
. Its intended use is mostly for ASP.NET pages (low number of client notifications).
ALTER DATABASE UrDb SET ENABLE_BROKER
实现 OnChange
事件以获得通知:
Implement the OnChange
event to get notified:
void OnChange(object sender, SqlNotificationEventArgs e)
在代码中:
SqlCommand cmd = ...
cmd.Notification = null;
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += OnChange;
它使用 Service Broker
(基于消息的通信平台)从数据库引擎接收消息.
It uses the Service Broker
(a message-based communication platform) to receive messages from the database engine.
这篇关于如何使用 c# 监控 SQL Server 表更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 c# 监控 SQL Server 表更改?


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01