Interrupt Errors in TClientDataSet.CommandText

I have a TClientDataSet connected to TDataSetProvider, which in turn is connected to TAdsQuery. I installed the SQL command and then open 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 does not work, however (syntax error or something else), I get an exception in the Advantage code, but it never gets into my own exception handling code.

Is there any way so that I can lure this error and report it to the user. Or is there a way to check the syntax of an SQL query before executing it?

I am using Delphi Pro 2009 and Advantage Local Server 9.

+3
source share
4 answers

Advantage includes an EADSDatabaseError that 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 using the VerifySQL method of the TAdsQuery component. This will throw an EADSDatabaseError exception if the SQL syntax is incorrect.

+3
source

Do you get an exception code (and not the exception you want) when you run it from the IDE, and also when run directly from your executable? The reason I'm asking is because the IDE will report an error / exception first, and if you don't continue, you will never see the actual exeception trap.

IDE , . , , , Delphi.

, , , , , , , . , , , , , .

try 
  CDS.CommandText := 'SELECT * FROM tablename WHERE fieldname = 1'; 
  CDS.Open 
except 
  on E: Exception do begin
    ShowMessage(E.Message);
    // optionally Exit, Abort or what else, to stop execution of the next statements
  end;
end;
+2

, TClientDataSet. , TAdsQuery.SQL, TClientDataSet, , .

, . " CommandText ".

procedure TForm57.Button1Click(Sender: TObject);
begin
  try
   CDS.CommandText := 'SELECT * FROM tablename WHERE fieldname = 1';
   CDS.Open;
  except
    on E : Exception do
      ShowMessage( 'got it:' + E.message );
  end;
end;
+1
  • SQL, TAdsQuery TAdsTable.
  • , TAdsTable . - " ".
  • :
    
        try 
          CDS.CommandText := 'SELECT * FROM tablename WHERE fieldname = 1'; 
          CDS.Open 
        except 
          on E: Exception do begin
            Application.HandleException(Self);
            // optionally Exit, Abort or what else, to stop execution of the next statements
          end;
        end;
    
  • SQL-. , . , , ( ).
0

Source: https://habr.com/ru/post/1745213/


All Articles