简体   繁体   English

将事件从存储过程引发到Windows服务

[英]Raise Event from store procedure to Windows Service

Is it possible to trigger an action in Windows Service, or is it possible to raise an event that can be caught in a Service from a stored procedure. 是否有可能在Windows Service中触发操作,还是有可能引发可以从存储过程捕获到Service的事件。

If you imagine a service runs every 5 minutes thats performs some action. 如果您想象每隔5分钟运行一次服务,那就可以执行某些操作。 What if something happens in a database or a stored procedure is executed and I want to trigger this action earlier than the 5 minutes. 如果数据库中发生了什么事或执行了存储过程,并且我想在5分钟之前触发此操作该怎么办?

Does anybody have any ideas? 有人有什么想法吗?

Thanks 谢谢

Ed 埃德

It sounds like you shouldn't be using a windows service at all. 听起来您根本不应该使用Windows服务。 I'd setup a SQL job to run every 5 minutes, then setup a trigger or whatever you expect to happen to run the job manually if necessary. 我会设置一个SQL作业每5分钟运行一次,然后设置一个触发器或您期望在需要时手动执行的任何操作。

EDIT: However, if the service is doing something outside of the database, you can use xp_cmdshell to run system commands (not the best option though). 编辑:但是,如果服务正在数据库之外做某事,则可以使用xp_cmdshell运行系统命令(尽管不是最佳选择)。

You can create a CLR procedure in SQL Server that somehow communicates with the service using SOAP, Remoting, or whatever medium. 您可以在SQL Server中创建CLR过程,该过程以某种方式使用SOAP,远程处理或任何其他媒介与服务进行通信。 This is generic though. 这是通用的。 I think query notification feature of SQL Server 2005 can be helpful here. 我认为SQL Server 2005的查询通知功能在这里可能会有所帮助。

如果您执行raiserror(@ ErrorMessage,16,1),只要您使用的严重性和状态分别为16和1,它就会在客户端生成一个异常。

Following on from Mehrdad's comments, I was able to solve the problem using the SqlDependancy Class in the .NET framework. 根据Mehrdad的评论,我能够使用.NET框架中的SqlDependancy类解决问题。

This Class allows you to register a SQL query and connection with it, when the result of that query changes an event is raised to indicate a change in the data. 当该查询的结果更改时,可以使用该类注册SQL查询并与其连接,从而引发一个事件来指示数据中的更改。

This allowed me to catch data changes almost instantaneously and deal with appropriately. 这使我几乎可以立即捕获数据更改并进行适当处理。

In order to use this Class you will need to Enable Query Notifications on the database you wish to query see: Enabling Query Notifications 为了使用该类,您将需要在要查询的数据库上启用查询通知,请参阅: 启用查询通知

A good example of how to use this class in a Windows Application is found here . 此处找到了如何在Windows应用程序中使用此类的一个很好的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM