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

查看:1708
本文介绍了如何监听用C#SQL Server表的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个应用程序访问同一个数据库,我需要,如果这些应用程序之一更改某个表的任何东西(UPDATE,INSERT)得到通知。

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.

推荐答案

您可以使用<$c$c>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 事件得到通知:

void OnChange(object sender, SqlNotificationEventArgs e)

和在code:

SqlCommand cmd = ...
cmd.Notification = null;

SqlDependency dependency = new SqlDependency(cmd);

dependency.OnChange += OnChange;

它使用的 服务代理 (一个基于消息的通信平台)从数据库引擎接收消息。

It uses the Service Broker (a message-based communication platform) to receive messages from the database engine.

这篇关于如何监听用C#SQL Server表的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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