Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (Deutsch) » UltraLite - .NET-Programmierung » UltraLite .NET 2.0 API-Referenz » ULCommand-Klasse

 

EndExecuteNonQuery-Methode

Beendet die asynchrone Ausführung einer SQL-Anweisung.

Syntax
Visual Basic
Public Function EndExecuteNonQuery( _
   ByVal asyncResult As IAsyncResult _
) As Integer
C#
public int EndExecuteNonQuery(
   IAsyncResult asyncResult
);
Parameter
  • asyncResult   Das System.IAsyncResult-Objekt, das vom Aufruf an BeginExecuteNonQuery zurückgegeben wird

Rückgabewert

Die Anzahl der betroffenen Zeilen (dasselbe Verhalten wie ExecuteNonQuery).

Bemerkungen

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:

EndExecuteNonQuery aufrufen 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 will block until the command completes
Dim rowCount As Integer = _
  cmd.EndExecuteNonQuery( res )

// C#
ULCommand cmd = new ULCommand(
    "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 );

Eigenschaft IsCompleted von IAsyncResult abrufen 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 )
  ' do other work
End While
' this will block until the command completes
Dim rowCount As Integer = _
  cmd.EndExecuteNonQuery( res )

// C#
ULCommand cmd = new ULCommand(
    "UPDATE Departments"
    + " SET DepartmentName = 'Engineering'"
    + " WHERE DepartmentID=100",
    conn
  );
IAsyncResult res = cmd.BeginExecuteNonQuery();
while( !res.IsCompleted ) {
  // do other work
}
// this will block until the command completes
int rowCount = cmd.EndExecuteNonQuery( res );

Eigenschaft IAsyncResult.AsyncWaitHandle verwenden, um ein Synchronisationsobjekt abzurufen 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 will not block because the command is finished
Dim rowCount As Integer = _
  cmd.EndExecuteNonQuery( res )

// 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 will not block because the command is finished
int rowCount = cmd.EndExecuteNonQuery( res );

Callback-Funktion beim Aufruf von BeginExecuteNonQuery angeben 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 
  ' will be called when the command completes
End Sub
// 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 
  // will be 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.

Siehe auch