EndExecuteNonQuery(IAsyncResult) method

Finishes asynchronous execution of a SQL statement or stored procedure.

Parameters

asyncResult
The IAsyncResult returned by the call to HanaCommand.BeginExecuteNonQuery.

Returns

The number of rows affected (the same behavior as HanaCommand.ExecuteNonQuery).

Exceptions

ArgumentException
The asyncResult parameter is null (Nothing in Visual Basic).
InvalidOperationException
The HanaCommand.EndExecuteNonQuery(IAsyncResult) was called more than once for a single command execution, or the method was mismatched against its execution method.

Remarks

Call EndExecuteNonQuery once for every call to BeginExecuteNonQuery. The call must be after BeginExecuteNonQuery has returned. ADO.NET is not thread safe; it is your responsibility to ensure that BeginExecuteNonQuery has returned. The IAsyncResult passed to EndExecuteNonQuery must be the same as the one returned from the BeginExecuteNonQuery call that is being completed. It is an error to call EndExecuteNonQuery to end a call to BeginExecuteReader, and vice versa.

If an error occurs while executing the command, then the exception is thrown when EndExecuteNonQuery is called.

There are four ways to wait for execution to complete:

(1) Call EndExecuteNonQuery.

Calling EndExecuteNonQuery blocks until the command completes. For example:

HanaConnection conn = new HanaConnection(
   "Server=hana-db:30015;UserID=JSmith;Password=secret");
conn.Open();
HanaCommand cmd = new HanaCommand( 
   "UPDATE Departments"
       + " SET DepartmentName = 'Engineering'"
       + " WHERE DepartmentID=100",
   conn );
IAsyncResult res = cmd.BeginExecuteNonQuery();
// perform other work
// this will block until the command completes
int rowCount = cmd.EndExecuteNonQuery( res );

(2) Poll the IsCompleted property of the IAsyncResult.

For example:

HanaConnection conn = new HanaConnection(
   "Server=hana-db:30015;UserID=JSmith;Password=secret");
conn.Open();
HanaCommand cmd = new HanaCommand( 
   "UPDATE Departments"
       + " SET DepartmentName = 'Engineering'"
       + " WHERE DepartmentID=100",
   conn );
IAsyncResult res = cmd.BeginExecuteNonQuery();
while( !res.IsCompleted ) {
   // do other work
}
// this will not block because the command is finished
int rowCount = cmd.EndExecuteNonQuery( res );

(3) Use the IAsyncResult.AsyncWaitHandle property to get a synchronization object, and wait on that.

For example:

HanaConnection conn = new HanaConnection(
   "Server=hana-db:30015;UserID=JSmith;Password=secret");
conn.Open();
HanaCommand cmd = new HanaCommand( 
   "UPDATE Departments"
       + " SET DepartmentName = 'Engineering'"
       + " WHERE DepartmentID=100",
   conn );
IAsyncResult res = cmd.BeginExecuteNonQuery();
// perform other work
WaitHandle wh = res.AsyncWaitHandle;
wh.WaitOne();
// this will not block because the command is finished
int rowCount = cmd.EndExecuteNonQuery( res );

(4) Specify a callback function when calling BeginExecuteNonQuery.

For example:

private void callbackFunction( IAsyncResult ar ) {
   HanaCommand cmd = (HanaCommand) ar.AsyncState;
   // this won't block since the command has completed
   int rowCount = cmd.EndExecuteNonQuery( ar );
}

// elsewhere in the code
private void DoStuff() {
   HanaConnection conn = new HanaConnection(
	"Server=hana-db:30015;UserID=JSmith;Password=secret");
   conn.Open();
   HanaCommand cmd = new HanaCommand(
       "UPDATE Departments"
           + " SET DepartmentName = 'Engineering'"
           + " WHERE DepartmentID=100",
       conn );
   IAsyncResult res = cmd.BeginExecuteNonQuery( callbackFunction, cmd );
   // perform other work.  The callback function is
   // called when the command completes
}

The callback function executes in a separate thread, so the usual caveats related to updating the user interface in a threaded program apply.