有什么样的FileSystemWatcher的对于SQL Server表? [英] Is there something like the FileSystemWatcher for Sql Server Tables?

查看:161
本文介绍了有什么样的FileSystemWatcher的对于SQL Server表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的窗口服务(将写在.NET)时,新的行添加到一个特定的表,但不是从SQL服务器把数据我想用一台服务器推模式来识别。

i would like my windows service (to be written in .NET) to recognize when new rows are added to a specific table, but instead of pulling the data from the sql-server i would like to use a server push model.

是否有人已经暗示我如何实现这一目标? 我使用SQL Server 2005。

does somebody has a hint for me how to achieve this? i am using sql server 2005.

TIA

推荐答案

另外还有一个ADO.NET 的SqlDependency 机制,如果你使用的是客户端ADO.NET与C#或VB.NET

There's also the ADO.NET SqlDependency mechanism if you're using client side ADO.NET with C# or VB.NET

一个的SqlDependency对象可以是   有一个SqlCommand,以便相关   检测时查询结果不同   从这些最初检索。您   也可以指定一个委托给   OnChange事件,这将触发时,   结果改变相关联   命令。您必须关联   的SqlDependency与之前的命令   您执行命令。该   HasChanges的财产   的SqlDependency也可以用来   确定该查询结果有   改变,因为该数据是第一   检索。

A SqlDependency object can be associated with a SqlCommand in order to detect when query results differ from those originally retrieved. You can also assign a delegate to the OnChange event, which will fire when the results change for an associated command. You must associate the SqlDependency with the command before you execute the command. The HasChanges property of the SqlDependency can also be used to determine if the query results have changed since the data was first retrieved.

您基本上与您的SqlCommand关联一个的SqlDependency ,并提供被调用值时,构成该的SqlDependency变化的结果集的事件处理程序。

You basically associate a SqlDependency with your SqlCommand, and provide an event handler that gets called when values that make up the result set of that SqlDependency change.

using(SqlCommand cmd = new SqlCommand(queryStatement, _conn))
{ 
   cmd.Notification = null;

   SqlDependency dependency = new SqlDependency(cmd);

   dependency.OnChange += 
       new OnChangeEventHandler(OnChange);

    ......
}

在事件处理程序,然后你可以做whathever你需要做的。

In the event handler, you can then do whathever you need to do.

void OnChange(object sender, SqlNotificationEventArgs e)
{
  SqlDependency dependency = sender as SqlDependency;

  (do whatever you need to do - e.g. reload the data)

}

马克·

这篇关于有什么样的FileSystemWatcher的对于SQL Server表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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