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

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

问题描述

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

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

我不想让我的用户在整个时间段内让应用程序保持打开状态.所以我想调用sproc,让它飞起来,让他们关闭应用程序,稍后再回来.

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 Activation: 你在数据库中创建一个服务并附加一个激活的程序.在您的应用程序(或 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天全站免登陆