完成 SQL 语句的异步执行。
Public Function EndExecuteNonQuery(
ByVal asyncResult As IAsyncResult
) As Integer
public int EndExecuteNonQuery(IAsyncResult asyncResult)
asyncResult 调用 BeginExecuteNonQuery 方法而返回的 System.IAsyncResult。
受影响的行数(与 ExecuteNonQuery 方法的行为相同)。
ArgumentException asyncResult 参数为空(在 Microsoft Visual Basic 中为 Nothing)。
InvalidOperationException 对一个命令执行多次调用 EndExecuteNonQuery(IAsyncResult) 方法,或者该方法与其执行方法不匹配。
每次调用 BeginExecuteNonQuery 时都必须调用一次 EndExecuteNonQuery 方法。必须在 BeginExecuteNonQuery 调用返回之后执行该调用。ADO.NET 不是线程安全的;必须确保 BeginExecuteNonQuery 调用已返回。传递给 EndExecuteNonQuery 方法的 System.IAsyncResult 必须与从即将完成的 BeginExecuteNonQuery 调用返回的 System.IAsyncResult 相同。通过调用 EndExecuteNonQuery 方法来结束对 BeginExecuteReader 方法的调用是错误的,反之亦然。
如果在执行命令时出现错误,则调用 EndExecuteNonQuery 方法时会抛出异常。
等待执行完成有四种方法:
调用 EndExecuteNonQuery 命令完成后再调用 EndExecuteNonQuery 块。例如:
' 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 ) |
以下代码为 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 ); |
轮询 IAsyncResult 的 IsCompleted 属性 可以轮询 IAsyncResult 的 IsCompleted 属性。例如:
' 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 ) |
以下代码为 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 ); |
使用 IAsyncResult.AsyncWaitHandle 属性获取同步对象 可以使用 IAsyncResult.AsyncWaitHandle 属性获取同步对象,并进行等待。例如:
' 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 ) |
以下代码为 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 ); |
调用 BeginExecuteNonQuery 方法时指定回调函数 可以在调用 BeginExecuteNonQuery 方法时指定回调函数。例如:
' 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 |
以下代码为 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. } |
回调函数在单独的线程中执行,因此与在线程化程序中更新用户界面有关的常见告诫也适用。
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |