Results 1 to 7 of 7

Thread: Running Stored Procedures in Async?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Question Running Stored Procedures in Async?

    Hi,

    I created a Sub in my WinForm which basically executes a stored procedure using Connections, Commands, Parameters etc. Command.CommandType is set to StoredProcedures always but command text may vary. Now I want to be able to run more than 1 StoredProcedure in parallel but I also need a way to terminate a running StoredProcedure if needed.

    What methods / approaches can I use for this requirement?

    I'm currently using Visual Studion 2008 Professional and Oracle Express Edition 11g for my database.

    Thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Running Stored Procedures in Async?

    The SqlClient provider for SQL Server that is built into the .NET Framework has the standard ExecuteReader and ExecuteNonQuery methods on its SqlCommand class, which are synchronous, and it also has asynchronous analogues: BeginExecuteReader/EndExecuteReader and BeginExecuteNonQuery/EndExecuteNonQuery. It also has the standard Cancel method. Hopefully you're using ODP.NET from Oracle, so you should check whether that has similar asynchronous methods on its OracleCommand class. If not then you will just have to use the standard synchronous methods and call them on a secondary thread, which you can start any of the usual ways, e.g. BackgroundWorker, ThreadPool, Thread, etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: Running Stored Procedures in Async?

    Hi jmcilhinney,

    Thanks for the suggestions.

    Yes I'm using ODP. I just checked and unfortunately the OracleCommand Class from ODP does not have this method. I am looking at Thread but still somehow challenged on how to be able to manage threads.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Running Stored Procedures in Async?

    Show me an example of how you would execute this sproc synchronously and I'll show you how to adapt that to execute is asynchronously.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Question Re: Running Stored Procedures in Async?

    Quote Originally Posted by jmcilhinney View Post
    Show me an example of how you would execute this sproc synchronously and I'll show you how to adapt that to execute is asynchronously.
    Hi jmcilhinney,

    This is what I had in mind so far. So basically I have a TextBox "ProcedureName" and Button "RunButton". This is just a sample of course. So basically I enter the stored procedure name in the TextBox and click the RunButton and enter another procedure name and click the button again and so on. I am using Thread to be able to do this however, my problem is if I have created several threads, I'm not sure how I can abort previous threads that I've created.

    Code:
    Sub StartProgram(ProcedureName As String)
        Using Connection As New Oracle.DataAccess.Client.OracleConnection(My.MySettings.Default.ConnectionString)
          Connection.Open()
          Using Command As New Oracle.DataAccess.Client.OracleCommand
            With Command
              .CommandType = CommandType.StoredProcedure
              .CommandText = ProcedureName
              .Connection = Connection
              .ExecuteNonQuery()
            End With
          End Using
          Connection.Close()
        End Using
      Catch ex As Exception
        Debug.WriteLine(ex.Message)
      End Try
    End Sub
    
    Public Sub RunButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunButton.Click
      Dim t As New System.Threading.Thread(AddressOf StartProgram)
      Dim args As Object = New Object() {ProcedureName.Text}
      t.Start(args)
    End Sub
    Thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Running Stored Procedures in Async?

    You don't want to abort threads. You want to call the Cancel method of the command object. As such, you'll need to store them somewhere that they can be accessed when required. That means that you'll need a collection of some sort assigned to a member variable. The StartProgram method can add the command it creates to that collection so that it can be accessed from another method on another thread to call its Cancel method. Once the command completes, it can be removed from the collection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: Running Stored Procedures in Async?

    Quote Originally Posted by jmcilhinney View Post
    You don't want to abort threads. You want to call the Cancel method of the command object. As such, you'll need to store them somewhere that they can be accessed when required. That means that you'll need a collection of some sort assigned to a member variable. The StartProgram method can add the command it creates to that collection so that it can be accessed from another method on another thread to call its Cancel method. Once the command completes, it can be removed from the collection.
    Will the command object have some sort of Unique Identifier that I can store in a database table? Currently this is what I'm doing when running actual Executable files initiated using Process object. Fortunately this returns a PID which I use later on when I need to kill the Process.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

Tags for this Thread

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