首页 > 解决方案 > 取消后重用 ADOQuery

问题描述

我在我的 Delphi 2010 应用程序中使用异步 ADO 查询。用户可以请求取消查询,否则可能会因错误而失败。这是我用来取消查询的代码:

if not Assigned(myADOQuery.Recordset) then exit;
if stFetching in myADOQuery.RecordsetState then begin
  fCommand := _Command(myADOQuery.Recordset.ActiveCommand);
  fCommand.Cancel;
  if Assigned(myADOQuery.Recordset) then myADOQuery.Recordset.Cancel;
end;
if Assigned(myADOQuery.Recordset) then myADOQuery.Recordset.Cancel;
StatusBar1.Panels[2].Text := '';
//ShowMessage('Query Cancelled');
myADOQuery.Close;

我正在使用 ADOConnection ExecuteComplete 向用户显示取消(或其他错误):

if EventStatus = esErrorsOccured then ShowMessage(Error.Description);

我现在希望能够在修改后重新使用查询,但是当我重新运行它时,我会收到相同的错误消息。有没有办法可以重置查询(包括 SQL.Text)并再次运行它?

尼穆斯

标签: delphiadodelphi-2010

解决方案


认为您的问题可能与您的查询的 Connection 对象有关。

AdoConnection 有一个Errors集合,该集合记录发生的每个 ADO 错误的详细信息。Iirc,此集合作为对象累积,使用 AdoConnection 遇到执行错误,直到您调用ClearErrors 接口。取消查询执行可能会导致下一次执行出错。

因此,在尝试重新使用您的 AdoQuery 之前尝试以下操作:

myAdoQuery.Connection.Errors.Clear;

让我们知道你的进展情况。

此外,如果我是你,我会在尝试重新打开之前测试 AdoQuery 的 RecordSet 是否为 NIL,并在取消例程结束时将其显式设置为 NIL。以防万一 ...


推荐阅读