Beendet die asynchrone Ausführung einer SQL-Anweisung.
Public Function EndExecuteNonQuery(
ByVal asyncResult As IAsyncResult
) As Integer
public int EndExecuteNonQuery(IAsyncResult asyncResult)
asyncResult Das System.IAsyncResult-Objekt, das vom Aufruf an BeginExecuteNonQuery zurückgegeben wird
Die Anzahl der betroffenen Zeilen (dasselbe Verhalten wie ExecuteNonQuery).
ArgumentException Der asyncResult-Parameter ist Null ("Nothing" in Microsoft Visual Basic).
InvalidOperationException EndExecuteNonQuery(IAsyncResult) wurde für eine einzelne Befehlsausführung mehrmals aufgerufen oder die Methode stimmte nicht mit der Ausführungsmethode überein.
EndExecuteNonQuery muss für jeden Aufruf von BeginExecuteNonQuery ein Mal aufgerufen werden. Der Aufruf muss erfolgen, nachdem BeginExecuteNonQuery ausgeführt wurde. ADO.NET ist nicht threadsicher. Sie müssen dafür Sorge tragen, dass BeginExecuteNonQuery abgeschlossen wurde. Das System.IAsyncResult-Objekt, das an EndExecuteNonQuery übergeben wurde, muss mit dem des ausgeführten BeginExecuteNonQuery-Aufrufs übereinstimmen. Es wird ein Fehler erzeugt, wenn EndExecuteNonQuery aufgerufen wird, um einen Aufruf von BeginExecuteReader zu beenden, und umgekehrt.
Wenn während der Ausführung des Befehls ein Fehler auftritt, wird eine Ausnahmebedingung generiert, wenn EndExecuteNonQuery aufgerufen wird.
Es gibt vier Möglichkeiten, auf den Abschluss der Ausführung zu warten:
Call EndExecuteNonQuery Der Aufruf von EndExecuteNonQuery wird blockiert, bis der Befehl ausgeführt wurde. Zum Beispiel:
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteNonQuery() ' Perform other work. ' This blocks until the command completes. Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) |
Dies entspricht dem folgenden Code in der Sprache C#:
// C# ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery(); // Perform other work. // This blocks until the command completes. int rowCount = cmd.EndExecuteNonQuery( res ); |
Poll the IsCompleted property of the IAsyncResult Sie können die Eigenschaft IsCompleted von IAsyncResult abrufen. Zum Beispiel:
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteNonQuery() While( !res.IsCompleted ) ' Perform other work. End While ' This blocks until the command completes. Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) |
Dies entspricht dem folgenden Code in der Sprache C#:
// C# ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery(); while( !res.IsCompleted ) { // Perform other work. } // This blocks until the command completes. int rowCount = cmd.EndExecuteNonQuery( res ); |
Use the IAsyncResult.AsyncWaitHandle property to get a synchronization object Sie können die Eigenschaft IAsyncResult.AsyncWaitHandle verwenden, um ein Synchronisationsobjekt abzurufen und darauf zu warten. Zum Beispiel:
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteNonQuery() ' Perform other work. Dim wh As WaitHandle = res.AsyncWaitHandle wh.WaitOne() ' This does not block because the command is finished. Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) |
Dies entspricht dem folgenden Code in der Sprache C#:
// C# ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery(); // perform other work WaitHandle wh = res.AsyncWaitHandle; wh.WaitOne(); // This does not block because the command is finished. int rowCount = cmd.EndExecuteNonQuery( res ); |
Specify a callback function when calling BeginExecuteNonQuery Sie können beim Aufruf von BeginExecuteNonQuery eine Callback-Funktion angeben. Zum Beispiel:
' Visual Basic Private Sub callbackFunction(ByVal ar As IAsyncResult) Dim cmd As ULCommand = _ CType(ar.AsyncState, ULCommand) ' This won't block since the command has completed. Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) End Sub ' Elsewhere in the code Private Sub DoStuff() Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult = _ cmd.BeginExecuteNonQuery( _ callbackFunction, cmd _ ) ' Perform other work. The callback function ' is called when the command completes. End Sub |
Dies entspricht dem folgenden Code in der Sprache C#:
// C# private void callbackFunction( IAsyncResult ar ) { ULCommand cmd = (ULCommand) ar.AsyncState; // This won't block since the command has completed. int rowCount = cmd.EndExecuteNonQuery(); } // Elsewhere in the code private void DoStuff() { ULCommand cmd = new ULCommand( "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. } |
Die Callback-Funktion wird in einem separaten Thread ausgeführt, sodass die üblichen Warnungen bezüglich der Aktualisierung der Benutzeroberfläche in einem Programm mit mehreren Threads gelten.
![]() |
Kommentieren Sie diese Seite in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |