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 类

 

EndExecuteNonQuery 方法

完成 SQL 语句或存储过程的异步执行。

语法
Visual Basic

Public Function EndExecuteNonQuery( _
   ByVal asyncResult As IAsyncResult _
) As Integer
C#

public int EndExecuteNonQuery(
   IAsyncResult asyncResult
);
参数
  • asyncResult   调用 SACommand.BeginExecuteNonQuery 而返回的 IAsyncResult。

返回值

受影响的行数(与 SACommand.ExecuteNonQuery 的行为相同)。

注释

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

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

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

(1) 调用 EndExecuteNonQuery。

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

SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
conn.Open();
SACommand cmd = new SACommand( 
  "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 reader = cmd.EndExecuteNonQuery( res );

(2) 轮询 IAsyncResult 的 IsCompleted 属性。

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

SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
conn.Open();
SACommand cmd = new SACommand( 
  "UPDATE Departments"
    + " SET DepartmentName = 'Engineering'"
    + " WHERE DepartmentID=100",
    conn
  );
IAsyncResult res = cmd.BeginExecuteNonQuery();
while( !res.IsCompleted ) {
// do other work
}
// this will not block because the command is finished
int rowCount = cmd.EndExecuteNonQuery( res );

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

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

SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
conn.Open();
SACommand cmd = new SACommand( 
  "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 );

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

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

private void callbackFunction( IAsyncResult ar )
{
   SACommand cmd = (SACommand) ar.AsyncState;
   // this won't block since the command has completed
            int rowCount = cmd.EndExecuteNonQuery();
}
// elsewhere in the code
private void DoStuff() 
{
      SAConnection conn = new SAConnection("DSN=SQL Anywhere 11 Demo");
      conn.Open();
            SACommand cmd = new SACommand(
        "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
}

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

另请参见