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# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 将数据集转换为列表 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
