MS Access:以异步方式运行存储过程 - 语法?

问题描述 投票:0回答:1

我在过去运行单线程SP时取得了很好的成功,等待“0”或“-1”回复表示成功,使用:

Set cnn = New ADODB.Connection
cnn.CursorLocation = adUseClient
cnn.ConnectionString = "driver={SQL Server};server=" & TempVars!my_ip & ";Trusted_Connection=no;Database=" & TempVars!my_Database & ";UID=username;PWD=password"
cnn.CommandTimeout = 0
cnn.Open

strSQL = "ExportToCSVZip 'myTable', '\\192.168.242.147\InventoryProcessing\Exports\', 'ProdCIVProcess', 'Y';"

Set rs = cnn.Execute(strSQL)

'SP Result -1 = Success, 0 = Fail
If rs.Fields(0) = -1 Then
    msgbox "Done"
Else
    msgbox "Fail"
End If

我可以使用以下方式异步运行:

cnn.Execute strSQL, adExecuteNoRecords, adAsyncExecute

但我不清楚这种形式的正确语法来确定Return标志。我的尝试返回了“序数”错误。

建议?

ms-access access-vba
1个回答
1
投票

根据您的确切配置,您可以非常轻松地返回参数。

假设您有以下SP:

CREATE PROCEDURE TestSP 
    @Param1 INT OUT
AS
    WAITFOR DELAY '00:00:03';
    SET @Param1 = 5;

然后,在您的模块中全局声明:

Dim conn As ADODB.Connection
Dim cmd As ADODB.Command

然后,在你的功能:

Set cnn = New ADODB.Connection
cnn.Open = "Some connectionstring"
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "TestSP"
Dim p As ADODB.Parameter
Set p = cmd.CreateParameter("@Param1", adInteger, adParamOutput, 8)
cmd.Parameters.Append p
Set cmd.ActiveConnection = conn
cmd.Execute Options:=adAsyncExecute

然后,在单独的函数中,测试命令是否完成,如果是,则返回该值

If cmd.State <> adStateExecuting Then
      returnValue = cmd.Parameters("@Param1").Value 'Returns 5 when done
End If
'Because of the global scope, cleanup is required
conn.Close
Set conn = Nothing
Set cmd = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.