dotConnect for SQL Server Documentation
Devart.Common Namespace / DbCommandBase Class / BeginExecuteNonQuery Method / BeginExecuteNonQuery(AsyncCallback,Object) Method
The delegate to call when the asynchronous invoke is complete. If callback is a null reference (Nothing in Visual Basic), the delegate is not called.
State information that is passed on to the delegate.
Example

In This Topic
    BeginExecuteNonQuery(AsyncCallback,Object) Method
    In This Topic
    Starts an asynchronous invocation of an ExecuteNonQuery method with information for callback function.
    Syntax
    'Declaration
     
    Public Overloads Function BeginExecuteNonQuery( _
       ByVal callback As AsyncCallback, _
       ByVal stateObject As Object _
    ) As IAsyncResult
    public IAsyncResult BeginExecuteNonQuery( 
       AsyncCallback callback,
       object stateObject
    )

    Parameters

    callback
    The delegate to call when the asynchronous invoke is complete. If callback is a null reference (Nothing in Visual Basic), the delegate is not called.
    stateObject
    State information that is passed on to the delegate.

    Return Value

    An System.IAsyncResult interface that represents the asynchronous operation started by calling this method.
    Remarks

    BeginExecuteNonQuery method enables you to execute a query on a server without having current thread blocked. In other words, your program can continue execution while the query runs on SQL Server so you do not have to wait for it.

    To start running a query, you have to call BeginExecuteNonQuery method, which in turn invokes appropriate actions in another thread. Return value of this method must be assigned to System.IAsyncResult object. After executing this method program flow continues.

    When you are ready to accept query results, call EndExecuteNonQuery. If at the moment you call this function the query execution has not yet been finished, application stops and waits till the function returns number of rows affected by the query.

    Refer to "Asynchronous Query Execution" article for detailed information.

    Example
    In the example below an easiest way of executing an asynchronous operation is shown. First, all components are set up and a connection is opened. Second, a query execution is started. The operation state (done or in progress) is checked and a report is written to the console. At last query result is rendered.
    static void Async_Exec(DbConnectionBase myConnection)
    {
      DbCommandBase myCommand = (DbCommandBase)myConnection.CreateCommand();
      myCommand.CommandText = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')";
      Console.WriteLine("Starting asynchronous query execution...");
      myConnection.Open();
      IAsyncResult cres = myCommand.BeginExecuteNonQuery(null, null);
      if (cres.IsCompleted)
        Console.WriteLine("Completed.");
      else
        Console.WriteLine("Have to wait for operation to complete...");
      try
      {
        int RowsAffected = myCommand.EndExecuteNonQuery(cres);
        Console.WriteLine("Done. Rows affected: " + RowsAffected.ToString());
      }
      catch (Exception e)
      {
        Console.WriteLine("An exception has occurred: " + e.Message);
      }
      finally
      {
        myConnection.Close();
      }
    }
    Public Sub Async_Exec(ByVal myConnection As DbConnectionBase)
      Dim myCommand As DbCommandBase = myConnection.CreateCommand()
      myCommand.CommandText = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')"
      Console.WriteLine("Starting asynchronous query execution...")
      myConnection.Open()
      Dim cres As IAsyncResult = myCommand.BeginExecuteNonQuery(Nothing, Nothing)
      If cres.IsCompleted Then
        Console.WriteLine("Completed.")
      Else
        Console.WriteLine("Have to wait for operation to complete...")
      End If
      Try
        Dim RowsAffected As Integer = myCommand.EndExecuteNonQuery(cres)
        Console.WriteLine(String.Concat("Rows affected: ", RowsAffected.ToString()))
      Catch e As Exception
        Console.WriteLine(String.Concat("An exception has occurred: ", e.Message))
      Finally
        myConnection.Close()
      End Try
    End Sub
    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also