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

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

问题描述

我在Windows应用程序中使用EF和我使用相同的数据库,这些新的记录将被一个网站插入结果想我的应用程序做一些任务时插入某个表的新纪录
我的问题是如何看这个表的变化,当一个新的记录来得到通知,并可以EF帮助我在这种情况下?

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?

更新:
我用的的SqlDependency类的并在数据库中使用此

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

ALTER DATABASE UrDb SET ENABLE_BROKER

和也创造了服务,并在数据库中 http://screencast.com/t/vrOjJbA1y但我从来没有从我的Windows应用程序的通知。结果
另外,当我打开SQL Server中的队列总是空空的 http://screencast.com/t/05UPDIwC8ck缝,有什么不对,但我不知道。

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.

推荐答案

您可以使用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.

您可能也想看看普通查询通知

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

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