2011-09-27 27 views
2

我从asp.net前端运行存储过程,但时间很长。在后台运行该事件的简单方法是什么?我的意思是,如果我关闭浏览器,我仍然希望我的存储过程完成,而不是死掉。另外,我想在我的程序运行时在前端执行其他操作。任何好的解决方案?在后台运行程序的简单方法

回答

2

SQL Agent和Service Broker都可以做到这一点,尽管它确实需要您做一些工作。

1

在另一个线程就启动它像这样:

'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