从Windows窗体异步执行存储过程,然后断开连接? [英] Execute a stored procedure from a windows form asynchronously and then disconnect?

查看:62
本文介绍了从Windows窗体异步执行存储过程,然后断开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从应用程序中调用存储过程,该过程可能需要30分钟才能执行.

I am calling a stored procedure from my application that can take 30 minutes to execute.

我不想让我的用户在整个时间段内都保持打开状态.因此,我想调用该存储过程,让它运行,然后让他们关闭该应用程序,然后再返回.

I don't want to make my user leave the application open for that entire time period. So I would like to call the sproc, let it fly, and let them shut down the application and come back later.

我该怎么做?

推荐答案

这实际上是一个很常见的场景.您无法执行任何基于客户端的操作,因为客户端可能会离开并断开连接,并且您将失去到目前为止所完成的工作.解决方案是使用 Service Broker激活 :您在数据库中创建了一个服务,并附加了一个已激活的过程.在您的应用程序(或ASP页面)中,您将一条消息发送到服务,并为过程嵌入必要的参数.提交应用程序后,该消息将激活服务过程.服务过程从消息中读取参数并调用您的过程.由于激活发生在与原始连接无关的服务器线程上,因此这是可靠的.实际上,服务器甚至可以在执行过程时 关闭并重新启动,并且工作将回滚然后恢复,因为激活消息将在重新启动后再次触发服务过程.

This is actually a quite common scenario. You cannot do anything client based because the client may go away and disconnect and you'll lose the work achieved so far. The solution is to use Service Broker Activation: you create a service in the database and attach an activated procedure. In your application (or ASP page) you send a message to the service and embed the necessary parameters for your procedure. After your application commits, the message activates the service procedure. the service procedure reads the parameters from the message and invokes your procedure. since activation happens on a server thread unrelated to your original connection, this is reliable. In fact the server can even shutdown and restart while your procedure is being executed and the work will be rolled back then resumed, since the activating message will trigger again the service procedure after the restart.

更新

我已经在我的博客上发布了如何执行此操作的详细信息,包括示例代码:

I have published the details of how to do this including sample code on my blog: Asynchronous procedure execution.

这篇关于从Windows窗体异步执行存储过程,然后断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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