如何使用c#监控SQL Server表的变化? [英] How to monitor SQL Server table changes by using c#?

查看:59
本文介绍了如何使用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表的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆