在后台运行程序的简单方法

发布于 2024-12-06 14:10:12 字数 121 浏览 0 评论 0原文

我正在从 asp.net 前端运行存储过程,但它很长。在后台运行该东西的简单方法是什么?我的意思是,如果我关闭浏览器,我仍然希望我的存储过程能够完成而不是直接死掉。另外,我想在程序运行时在前端执行其他操作。有什么好的解决办法吗?

I'm running stored procedure from asp.net front-end but it's quite long. What is the easy way on running that thing in the background? I mean if I close the browser, I still want my stored procedure to complete not just die. Also, I would like to perform other actions on the front-end while my procedure is running. Any good solution for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总以为 2024-12-13 14:10:13

只需在另一个线程中启动它,如下所示:

'Starts execution of the proc
 Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim t As New Threading.Thread(AddressOf DoWork)
        t.Start()
    End Sub

 Private Sub DoWork()
        Using c As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            c.Open()
            Dim command = New SqlCommand("LongTest", c)
            command.CommandType=Data.CommandType.StoredProcedure
            command.CommandTimeout = 0
            command.ExecuteNonQuery()
        End Using
    End Sub

这是我用于测试的 sp:

create PROCEDURE dbo.LongTest
AS
BEGIN
   WaitFor Delay '00:00:30' --wait for 30 seconds before doing anything
   insert into TableImageTest(image)
   values(null)

END

Just launch it in another Thread as so:

'Starts execution of the proc
 Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim t As New Threading.Thread(AddressOf DoWork)
        t.Start()
    End Sub

 Private Sub DoWork()
        Using c As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            c.Open()
            Dim command = New SqlCommand("LongTest", c)
            command.CommandType=Data.CommandType.StoredProcedure
            command.CommandTimeout = 0
            command.ExecuteNonQuery()
        End Using
    End Sub

Here's the sp that I used for my test:

create PROCEDURE dbo.LongTest
AS
BEGIN
   WaitFor Delay '00:00:30' --wait for 30 seconds before doing anything
   insert into TableImageTest(image)
   values(null)

END
拥抱没勇气 2024-12-13 14:10:13

在连接字符串中使用“异步处理=true”并异步调用存储过程,如本链接所示。

http://geekswithblogs.net/frankw /archive/2008/07/05/execute-transact-sql-statement-asynchronously.aspx

Use "Asynchronous Processing=true" in your connection string and call the sproc asynchronously as in this link.

http://geekswithblogs.net/frankw/archive/2008/07/05/execute-transact-sql-statement-asynchronously.aspx

王权女流氓 2024-12-13 14:10:12

SQL 代理和 Service Broker 都可以执行此操作,尽管这需要您做一些工作。

Both SQL Agent and the Service Broker can do this, though it does take some work on your part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文