概述
In order to run a stored procedure asynchronously and capture the moment when the procedure terminates, I am using a class module combined with ADO. The particular stored procedure in this example 'sp_DPSJA' collates data from other databases hence the slow execution. It has only one argument, @bom which is the first day of a particular month for which the data is being collated.
为了异步运行存储过程并捕获该过程终止的时刻,我将使用与ADO结合使用的类模块。 此示例中的特定存储过程'sp_DPSJA'整理来自其他数据库的数据,因此执行缓慢。 它只有一个参数@bom,它是要整理数据的特定月份的第一天。
The class module is called clsJSA and is as follows:
该类模块称为clsJSA,如下所示:
Option Compare Database
Option Explicit
'ADODB connection object, note withevents so we can capture the completion
Private WithEvents mcn As ADODB.Connection
'Execution flag
Dim mblnExecuted As Boolean
Private Sub Class_Initialize()
Set mcn = New ADODB.Connection
'GetADOCS just compiles a standard SQL ADO connection string in the form
'Provider=sqloledb;Data Source=<server>;Initial Catalog=<database>;User Id=<user ID>;Password=<password>
mcn.ConnectionString = GetADOCS()
mcn.Open
End Sub
Private Sub mcn_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
Dim strMsg As String
mblnExecuted = True
End Sub
Function AggregateJF(datFOM As Date) As Boolean
Dim strCMD As String
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
On Error GoTo proc_err
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = mcn
'Set up the command
cmd.CommandTimeout = 0
cmd.CommandText = "sp_DPSJA"
cmd.CommandType = adCmdStoredProc
'apply the date
Set prm = cmd.CreateParameter("@bom", adDate, adParamInput, , Format(datFOM, "dd/mmm/yyyy")): cmd.Parameters.Append prm
'run teh procedure asychronously
cmd.Execute Options:=adExecuteNoRecords + adAsyncExecute
proc_exit_true:
AggregateJF = True
proc_exit:
Exit Function
proc_exit_false:
AggregateJF = False
GoTo proc_exit
'error handler code
proc_err:
Select Case ErrHand()
Case ErrAbort
Resume proc_exit_false
Case ErrRetry
Resume
Case ErrIgnore
Resume Next
End Select
End Function
Property Get Executed() As Boolean
'retrieve execution flag
Executed = mblnExecuted
End Property
Note that with ADO only the Connection object allows events, Command and other subordinate objects do not (afaik).
请注意,对于ADO,仅Connection对象允许事件,Command和其他从属对象则不允许(afaik)。
Now the form: when the form is loaded it checks the OpenArgs parameter (which should be the first day of a particular month) and kicks off the stored procedure using clsJSA then sets the timer interval so a message can be flashed.
现在是表单:加载表单时,它将检查OpenArgs参数(应该是特定月份的第一天),并使用clsJSA启动存储过程,然后设置计时器间隔,以便可以刷新消息。
Option Compare Database
Option Explicit
Dim jsa As clsJSA
Private Sub Form_Load()
Dim datFOM As Date
'The calling procedure passes the date via openargs
If Not IsBlank(Me.OpenArgs) Then
'Capture the date
datFOM = CDate(Me.OpenArgs)
'Set the caption showing the month and year
Me.lblMY.Caption = "For " & Format(datFOM, "mmmm yyyy")
'Instantiate the class
Set jsa = New clsJSA
'tell the class to execute the SP
jsa.AggregateJF datFOM
'Set the timer interval for the form (two seconds) and start the hourglass
Me.TimerInterval = 2000
HGON
End If
End Sub
Private Sub Form_Timer()
'If the SP has finished the switch off the hourglass and close the form
If jsa.Executed Then
HGOFF
DoCmd.Close acForm, Me.Name
Else
'Otherwise flash the message
If Me.lblPW.Visible = True Then
Me.lblPW.Visible = False
Else
Me.lblPW.Visible = True
End If
Me.Repaint
End If
End Sub
I should perhaps point out that some of the functions here e.g. Isblank, HGON, HGOFF are my own but I think it's pretty obvious what they do :)
我也许应该指出,这里的某些功能(例如Isblank,HGON,HGOFF)是我自己的,但我认为它们的作用很明显:)
翻译自: https://www.experts-exchange.com/articles/34187/How-to-run-a-SQL-Server-stored-procedure-asynchronously-from-Microsoft-Access.html
最后
以上就是重要夏天为你收集整理的如何从Microsoft Access异步运行SQL Server存储过程的全部内容,希望文章能够帮你解决如何从Microsoft Access异步运行SQL Server存储过程所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复