Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TMS Aurelius
Executing SQL statements directly with IDBConnection

You might have some situations where you want to execute an SQL statement directly in the database, bypassing the object manager. Of course you could use the database access component directly (FireDac, dbExpress, ADO, etc.). But in case you want to keep the code abstract when it comes to the database access layer, and benefit from existing Aurelius connection, you can use IDBConnection to do so.

Here is how you would do it:

uses {...}, Aurelius.Drivers.Interfaces;

var
  Statement: IDBStatement;

Statement := Manager.Connection.CreateStatement;
Statement.SetSQLCommand(TheSQLStatement);
Statement.Execute;

If the statement is queryable, you can retrieve results using ExecuteQuery method which returns an IDBResultSet interface:

var
  Statement: IDBStatement;
  ResultSet: IDBResultSet;

Statement := Manager.Connection.CreateStatement;
Statement.SetSQLCommand(TheSQLStatement);
ResultSet := Statement.ExecuteQuery;

while ResultSet.Next do
  Value := ResultSet.GetFieldValue(SomeFieldName);

Finally, for both types of commands you can set parameter values:

{...}
Params := TObjectList<TDBParam>.Create;
try
  Statement := Manager.Connection.CreateStatement; 
  Params.Add(TDBParam.Create('id', ftInteger, 15));
  Statement.SetSQLCommand('Delete from Customer Where Id = :id');
  Statement.SetParams(Params);
  Statement.Execute;
finally
  Params.Free;
end;