我正在将应用程序从 BDE 转换为 ADO。
在 BDE 下,如果查询是打开的并且您调用了“Sql.Clear”,它将自动关闭数据集。
但是,在 TADOQuery 下情况并非如此,它会引发异常“无法在关闭的数据集上执行操作”。
我们的很多遗留代码都依赖于旧的 BDE 行为,所以我从代码中得到很多运行时错误,如下例所示。
我想覆盖我的 TADOCustomQuery 类的 Sql.Clear 方法,以便它包含一个“.Close”命令。我怎样才能做到这一点?
“.Clear”方法在 SQL 属性上,该属性是 TWideStrings 类型。我真正的问题是:如何在 TADOQuery 的后代上覆盖 TWideStrings.Clear 方法?
我已经有一个自定义的 TADOQuery 组件,它用于 SQL 属性:
property SQL: TWideStrings read GetSQL write SetSQL;
这是一些代码来演示我遇到的问题:
procedure TForm1.btnBDEDemoClick(Sender: TObject);
var
qryBDE: TQuery;
begin
//Both queries complete with no problem
qryBDE := TQuery.Create(nil);
try
with qryBDE do begin
DatabaseName := 'Test'; //BDE Alias
Sql.Clear;
Sql.Add('SELECT SYSDATE AS CURDAT FROM DUAL');
Open;
ShowMessage('the current date is: ' + FieldByName('CURDAT').AsString);
Sql.Clear; //<<<<<NO ERRORS, WORKS FINE
Sql.Add('SELECT SYSDATE-1 AS YESDAT FROM DUAL');
Open;
ShowMessage('And yesterday was: ' + FieldByName('YESDAT').AsString);
end; //with qryBDE
finally
FreeAndNil(qryBDE);
end; //try-finally
end;
procedure TForm1.btnADODemoClick(Sender: TObject);
const
c_ConnString = 'Provider=OraOLEDB.Oracle.1;Password=*;'+
'Persist Security Info=True;User ID=*;Data Source=*';
var
adoConn: TADOConnection;
qryADO: TADOQuery;
begin
//First query completes, but the second one FAILS
adoConn := TADOConnection.Create(nil);
qryADO := TADOQuery.Create(nil);
try
adoConn.ConnectionString := c_ConnString;
adoConn.Connected := True;
with qryADO do begin
Connection := adoConn;
Sql.Clear;
Sql.Add('SELECT SYSDATE AS CURDAT FROM DUAL');
Open;
ShowMessage('the current date is: ' + FieldByName('CURDAT').AsString);
Sql.Clear;//<<<<<<<<===========ERROR AT THIS LINE
Sql.Add('SELECT SYSDATE-1 AS YESDAT FROM DUAL');
Open;
ShowMessage('And yesterday was: ' + FieldByName('YESDAT').AsString);
end; //with qryADO
finally
FreeAndNil(qryADO);
FreeAndNil(adoConn);
end; //try-finally
end;