I am converting an application from BDE to ADO.
Under the BDE, if a query was Open and you called "Sql.Clear", it would automatically close the dataset.
However, this is not the case under TADOQuery, where it will raise an exception "Operation cannot be performed on a closed dataset".
A lot of our legacy code relies on the old BDE behavior, so I get lots of runtime errors from code like the example below.
I want to override the Sql.Clear method of my TADOCustomQuery class, so that it will include a ".Close" command. How can I do that?
The ".Clear" method is on the SQL property, which is of type TWideStrings. My real question is: how can I override the TWideStrings.Clear method on a descendant of TADOQuery?
I have a customized TADOQuery component already, with this for the SQL property:
property SQL: TWideStrings read GetSQL write SetSQL;
Here is some code to demonstrate the problem I'm having:
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;