在sql数据库中观察表中的新记录 [英] Watch for a table new records in sql database

查看:219
本文介绍了在sql数据库中观察表中的新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用程序中使用EF,而当某个表格中插入新的记录时,我希望我的应用程序执行某些任务这些新记录将被网站使用相同的数据库插入

我的问题是如何观看这个表格的变化,并在新记录到来时得到通知,EF可以帮助我吗?



更新: / strong>
我使用 SqlDependency类,并在db中使用这个。



ALTER DATABASE UrDb SET ENABLE_BROKER



还在数据库中创建了一个服务和一个队列 http://screencast.com/t/vrOjJbA1y ,但我从来没有收到我的Windows应用程序的通知。

当我在sql server中打开队列它总是空的 http://screencast.com/t/05UPDIwC8ck 接缝有问题,但我不知道。

解决方案

您可以使用 SqlDependency Class 。它的预期用途主要是ASP.NET页面(客户端通知数量不多)。

  ALTER DATABASE UrDb SET ENABLE_BROKER 

实现 OnChange 事件以获得通知:

  void OnChange(object sender,SqlNotificationEventArgs e)

在代码中:

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

SqlDependency dependency = new SqlDependency(cmd);

dependency.OnChange + = OnChange;

它使用 Service Broker (基于消息的通信平台)从数据库引擎收到消息。



您可能还想查看查询通知


I am using EF in a windows application and I want my application to do some tasks when a new record inserted in a certain table "these new records will be inserted by a website using the same db"
My question is how to watch this table for changes and get notified when a new record come, and can EF help me in this case?

UPDATE: I used the SqlDependency Class and used this in the db

ALTER DATABASE UrDb SET ENABLE_BROKER

And also created a service and a queue in the database http://screencast.com/t/vrOjJbA1y but I never get notified from my windows application.
Also the when i open the queue in sql server it is always empty http://screencast.com/t/05UPDIwC8ck seams that there is something wrong but i don't know.

解决方案

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

Implement the OnChange event to get notified:

void OnChange(object sender, SqlNotificationEventArgs e)

And in 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.

You may also want to look at Query Notification in general.

这篇关于在sql数据库中观察表中的新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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