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

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - 编程 » SQL Anywhere 数据访问 API » SQL Anywhere .NET 2.0 API 参考 » iAnywhere.Data.SQLAnywhere 命名空间 (.NET 2.0) » SACommand 类

 

EndExecuteReader 方法

完成 SQL 语句或存储过程的异步执行,并返回所请求的 SADataReader。

语法
Visual Basic

Public Function EndExecuteReader( _
   ByVal asyncResult As IAsyncResult _
) As SADataReader
C#

public SADataReader EndExecuteReader(
   IAsyncResult asyncResult
);
参数
  • asyncResult   调用 SACommand.BeginExecuteReader 而返回的 IAsyncResult。

返回值

可用于检索所请求行的 SADataReader 对象(与 SACommand.ExecuteReader 的行为相同)。

注释

每次调用 BeginExecuteReader 时都必须调用一次 EndExecuteReader。必须在 BeginExecuteReader 返回之后执行该调用。ADO.NET 不是线程安全的;确保 BeginExecuteReader 已返回是您的责任。传递给 EndExecuteReader 的 IAsyncResult 必须与从正在完成的 BeginExecuteReader 调用返回的 IAsyncResult 相同。通过调用 EndExecuteReader 来结束对 BeginExecuteNonQuery 的调用是错误的,反之亦然。

如果在执行命令时出现错误,则调用 EndExecuteReader 时会抛出异常。

等待执行完成有四种方法:

(1) 调用 EndExecuteReader。

在命令完成之前会阻止调用 EndExecuteReader。例如:

SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
conn.Open();
SACommand cmd = new SACommand( "SELECT * FROM Departments", 
  conn );
IAsyncResult res = cmd.BeginExecuteReader();
// perform other work
// this will block until the command completes
SADataReader reader = cmd.EndExecuteReader( res );

(2) 轮询 IAsyncResult 的 IsCompleted 属性。

可以轮询 IAsyncResult 的 IsCompleted 属性。例如:

SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
conn.Open();
SACommand cmd = new SACommand( "SELECT * FROM Departments", 
  conn );
IAsyncResult res = cmd.BeginExecuteReader();
while( !res.IsCompleted ) {
// do other work
}
// this will not block because the command is finished
SADataReader reader = cmd.EndExecuteReader( res );

(3) 使用 IAsyncResult.AsyncWaitHandle 属性获取同步对象。

可以使用 IAsyncResult.AsyncWaitHandle 属性获取同步对象,并就此进行等待。例如:

SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
conn.Open();
SACommand cmd = new SACommand( "SELECT * FROM Departments", 
  conn );
IAsyncResult res = cmd.BeginExecuteReader();
// perform other work
WaitHandle wh = res.AsyncWaitHandle;
wh.WaitOne();
// this will not block because the command is finished
SADataReader reader = cmd.EndExecuteReader( res );

(4) 调用 BeginExecuteReader 时指定回调函数。

可在调用 BeginExecuteReader 时指定回调函数。例如:

private void callbackFunction( IAsyncResult ar )
{
   SACommand cmd = (SACommand) ar.AsyncState;
   // this won’t block since the command has completed
            SADataReader reader = cmd.EndExecuteReader();
}
// elsewhere in the code
private void DoStuff() 
{
      SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
      conn.Open();
            SACommand cmd = new SACommand( "SELECT * FROM Departments", 
        conn );
   IAsyncResult res = cmd.BeginExecuteReader( callbackFunction, cmd );
   // perform other work.  The callback function will be 
   // called when the command completes
}

回调函数在单独的线程中执行,因此与在线程化程序中更新用户界面有关的常见告诫也适用。

另请参见