完成 SQL 语句的异步执行。
Visual Basic Public Function EndExecuteNonQuery( _ ByVal asyncResult As IAsyncResult _ ) As Integer
C# public int EndExecuteNonQuery( IAsyncResult asyncResult );
asyncResult 调用 BeginExecuteNonQuery 而返回的 System.IAsyncResult。
受影响的行数(与 ExecuteNonQuery 的行为相同)。
每次调用 BeginExecuteNonQuery 时都必须调用一次 EndExecuteNonQuery。必须在 BeginExecuteNonQuery 返回之后执行该调用。ADO.NET 不是线程安全的;由您负责来确保 BeginExecuteNonQuery 已返回。传递给 EndExecuteNonQuery 的 System.IAsyncResult 必须与从即将完成的 BeginExecuteNonQuery 调用返回的 System.IAsyncResult 相同。通过调用 EndExecuteNonQuery 来结束对 BeginExecuteReader 的调用是错误的,反之亦然。
如果在执行命令时出现错误,则调用 EndExecuteNonQuery 时会抛出异常。
等待执行完成有四种方法:
Call 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 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 ); |
Poll the IsCompleted property of the IAsyncResult 可以轮询 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 ) ' 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 ); |
Use the IAsyncResult.AsyncWaitHandle property to get a synchronization object 可以使用 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 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 ); |
Specify a callback function when calling 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 ' 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 } |
回调函数在单独的线程中执行,因此与在线程化程序中更新用户界面有关的常见告诫也适用。
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |