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 表更改?
基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
