I have a TClientDataSet connected to a TDataSetProvider, which in turn is connected to a TAdsQuery. I set the SQL command and then open the ClientDataset something like this:
try
CDS.CommandText := 'SELECT * FROM tablename WHERE fieldname = 1';
CDS.Open
except
// trap exception here - this never gets executed!
end;
If the SQL statement in CommandText fails, however (syntax error or whatever) I get an exception within the Advantage code, but it never gets caught in my own exception handling code.
Is there any way for me to trap this error and report it nicely to the user. Alternatively is there a way to verify the syntax of an SQL query before executing it?
I'm using Delphi Pro 2009, and Advantage Local Server 9.
Advantage includes a EADSDatabaseError which will provide more information about the exception that was raised.
try
CDS.CommandText := 'SELECT * FROM tablename WHERE fieldname = 1';
CDS.Open
except
on E: EDatabaseError do
begin
if ( E is EADSDatabaseError ) then
begin
ErrorString := (E as EADSDatabaseError).SQLErrorCode + E.Message;
application.messagebox ( pchar(ErrorString), 'Advantage Database Error', 0 )
end
else
application.messagebox (pchar(E.message), 'Native Database Error', 0 );
end;
end;
You can check the syntax of the SQL statement before executing it by using the VerifySQL method of the TAdsQuery component. This will raise an EADSDatabaseError exception if the SQL syntax is incorrect.