SQL 文の非同期実行を終了します。
Public Function EndExecuteNonQuery(
ByVal asyncResult As IAsyncResult
) As Integer
public int EndExecuteNonQuery(IAsyncResult asyncResult)
asyncResult BeginExecuteNonQuery メソッドへの呼び出しによって返される System.IAsyncResult。
影響を受けるローの数。これは、ExecuteNonQuery メソッドと同じ動作です。
ArgumentException asyncResult パラメータが null (Microsoft Visual Basic の Nothing) です。
InvalidOperationException 1 回のコマンド実行に対して EndExecuteNonQuery(IAsyncResult) メソッドが複数回呼び出されたか、このメソッドがその実行メソッドと一致しませんでした。
BeginExecuteNonQuery を呼び出すたびに、EndExecuteNonQuery メソッドを呼び出す必要があります。呼び出しは、BeginExecuteNonQuery 呼び出しが返された後に行う必要があります。ADO.NET はスレッドに対応していないため、BeginExecuteNonQuery 呼び出しが返されたことを確認する必要があります。EndExecuteNonQuery メソッドに渡される System.IAsyncResult は、完了した BeginExecuteNonQuery 呼び出しから返されるものと同じでなければなりません。EndExecuteNonQuery メソッドを呼び出して、BeginExecuteReader メソッドへの呼び出しを終了すると、エラーになります。その逆も同様です。
コマンドの実行中にエラーが発生すると、EndExecuteNonQuery メソッドが呼び出されるときに例外がスローされます。
実行の完了を待機するには、4 通りの方法があります。
「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 で意見交換できます
|
Copyright © 2013, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0 |