Results 1 to 8 of 8

Thread: run stored procedure asynchronously *Resolved*

  1. #1

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    run stored procedure asynchronously *Resolved*

    I made a post yesterday called "Application Busy" asking about how to handle a stored procedure that runs for up to twenty minutes locking up my application. Someone I was talking to about it mentioned I could run stored procedure asynchronously from an ActiveX exe and another said just run it run asynchronously.

    I Googled on it and it looks like it has potential but everything I saw was about .NET.

    Can I use the method in 6.0? Does any one have a sample project or could post some code to get me started?
    Last edited by TysonLPrice; Aug 22nd, 2005 at 06:17 AM.

  2. #2
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: run stored procedure asynchronously

    ActiveX Exe... or DLL... As long as you execute the procedure/sub/function which is late binded it kinda multithreads.

    VB Code:
    1. Dim obj as Object
    2. set obj = CreateObject("MyApp.MyClass")
    3. obj.InitApplication

    Something like that; In the class inside the DLL or EXE just activate a timer and when the timer activates just disable it again but initiate the sub/function that does the 20 minute run.

  3. #3

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: run stored procedure asynchronously

    Quote Originally Posted by Devion
    ActiveX Exe... or DLL... As long as you execute the procedure/sub/function which is late binded it kinda multithreads.

    VB Code:
    1. Dim obj as Object
    2. set obj = CreateObject("MyApp.MyClass")
    3. obj.InitApplication

    Something like that; In the class inside the DLL or EXE just activate a timer and when the timer activates just disable it again but initiate the sub/function that does the 20 minute run.
    That really doesn't tell me anything. Could you dummy it up a bit for me. Do you have an example that works?

  4. #4
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: run stored procedure asynchronously

    Make an ActiveX DLL Project...

    - Give it a projectname...
    - Name the class..
    - Add a Form (Very easy) or Timer class.
    - The class needs to have a sub or function called 'InitApplication' if you want to follow the code above.
    - The InitApplication function or sub calls the timer on either the form or the timer class and activates it.
    - Disable the timer before you start the real code
    - The timer then when it triggers will start your real code that takes 20 minutes.

    The reason why you have to do this is because the timer is self-driven and not called from your main code which will continue after initApplication with it's own code.

    Hope that dummies it up a bit

  5. #5
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: run stored procedure asynchronously

    You might like to look at using asynchronous COM+. You'd need to specify the interface in a typelib, but that shouldn't be too much hassle.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: run stored procedure asynchronously

    You can execute ADO commands asynchronously.

    How are you executing the query now?

    Specify adAsyncExecute in the Options agrument of the Connection.Execute or Command.Execute method. Something like the following might work. I don't have any long running queries to be able to test properly.

    VB Code:
    1. Private WithEvents db As ADODB.Connection
    2.  
    3. Private Sub Command1_Click()
    4.  
    5. db.Open "provider=sqloledb;data source=trixie;initial catalog=northwind;integrated security=sspi"
    6.  
    7. db.Execute "SprocName", , adCmdStoredProc Or adAsyncExecute
    8.  
    9. End Sub
    10.  
    11. Private Sub db_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, _
    12.     adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, _
    13.     ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
    14.  
    15.      'code to execute when procedure has completed.
    16.  
    17. End Sub

  7. #7

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: run stored procedure asynchronously

    I'm using this currently:

    sConnString = "Server=COMMONRPTSQL;Database=TEAMCMI;Driver=SQL Server;Trusted_Connection=Yes;DSN=COMMONRPTSQL"

    sSql = "exec rptAON"

    With Cmd
    .CommandType = adCmdText
    .CommandText = sSql
    .ActiveConnection = conn
    .CommandTimeout = 0
    DoEvents
    Set rsAONReport = .Execute
    DoEvents
    End With

    I tried plugging your code into a test form and I get the error:

    "Object does not source automation events"

    What you posted seems like what I want. Do you know what may be causing that error?

  8. #8

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: run stored procedure asynchronously

    Thanks everyone. I found a good example in the help library. I'm posting it in case someone searchs on this topic.

    Add the Microsoft ActiveX Data Objects 2.7 (that's what I picked). Then you should just be able to paste this in:

    VB Code:
    1. 'BeginStateVB
    2.  
    3.     'To integrate this code
    4.     'replace the data source and initial catalog values
    5.     'in the connection string
    6.  
    7. Public Sub Main()
    8.     On Error GoTo ErrorHandler
    9.  
    10.     Dim Cnxn1 As ADODB.Connection
    11.     Dim Cnxn2 As ADODB.Connection
    12.     Dim cmdChange As ADODB.Command
    13.     Dim cmdRestore As ADODB.Command
    14.     Dim strCnxn As String
    15.     Dim strSQL As String
    16.    
    17.     ' Open two asynchronous connections, displaying
    18.     ' a message while connecting
    19.     Set Cnxn1 = New ADODB.Connection
    20.     Set Cnxn2 = New ADODB.Connection
    21.     strCnxn = "Provider='SQLOLEDB';Data Source='CMISQL';" & _
    22.         "Initial Catalog='Pubs';Integrated Security='SSPI';"
    23.        
    24.     Cnxn1.Open strCnxn, , , adAsyncConnect
    25.     Do Until Cnxn1.State <> adStateConnecting
    26.        Debug.Print "Opening first connection...."
    27.     Loop
    28.    
    29.     Cnxn2.Open strCnxn, , , adAsyncConnect
    30.     Do Until Cnxn2.State <> adStateConnecting
    31.        Debug.Print "Opening second connection...."
    32.     Loop
    33.    
    34.     ' Create two command objects
    35.     Set cmdChange = New ADODB.Command
    36.     cmdChange.ActiveConnection = Cnxn1
    37.     strSQL = "UPDATE Titles SET type = 'self_help' WHERE type = 'psychology'"
    38.     cmdChange.CommandText = strSQL
    39.    
    40.     Set cmdRestore = New ADODB.Command
    41.     cmdRestore.ActiveConnection = Cnxn2
    42.     strSQL = "UPDATE Titles SET type = 'psychology' WHERE type = 'self_help'"
    43.     cmdRestore.CommandText = strSQL
    44.     cmdChange.Execute , , adAsyncExecute
    45.     ' Executing the commands, displaying a message
    46.     ' while they are executing
    47.    
    48.     Do Until cmdChange.State <> adStateExecuting
    49.        Debug.Print "Change command executing...."
    50.     Loop
    51.    
    52.     cmdRestore.Execute , , adAsyncExecute
    53.     Do Until cmdRestore.State <> adStateExecuting
    54.        Debug.Print "Restore command executing...."
    55.     Loop
    56.  
    57.     ' clean up
    58.     Cnxn1.Close
    59.     Cnxn2.Close
    60.     Set Cnxn1 = Nothing
    61.     Set Cnxn2 = Nothing
    62.     Exit Sub
    63.    
    64. ErrorHandler:
    65.     ' clean up
    66.     If Not Cnxn1 Is Nothing Then
    67.         If Cnxn1.State = adStateOpen Then Cnxn1.Close
    68.     End If
    69.     Set Cnxn1 = Nothing
    70.    
    71.     If Not Cnxn2 Is Nothing Then
    72.         If Cnxn2.State = adStateOpen Then Cnxn2.Close
    73.     End If
    74.     Set Cnxn2 = Nothing
    75.    
    76.     If Err <> 0 Then
    77.         MsgBox Err.Source & "-->" & Err.Description, , "Error"
    78.     End If
    79. End Sub
    80. 'EndStateVB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width