Additional
Polls
What you would like to see in next AnyDAC versions ?
 
Less coding with AnyDAC
Posted by Diman [Aug 24, 2009]
The classic routine data access tasks requires less coding with AnyDAC, than with any other data access library. Than less to code, than programmers are more fast and happy :)

I often see a code, which looks like:

 

q := TADOQuery.Create(nil);
q.Connection := conn;
try
  q.SQL.Clear;
  q.SQL.Add('select id from MyTab where name = :p1 and addr = :p2');
  q.Parameters.CreateParameter('p1', ftString, pdInput, 255, edtName.Text);
  q.Parameters.CreateParameter('p2', ftString, pdInput, 255, edtAddr.Text);
  q.Prepare;
  q.Open;
  sId := q.FieldByName('id').AsString;
finally
  q.Free;
end;

 

While some lines there are optional, most of them are really optional with AnyDAC ! In first option I will use the overloaded TADQuery.Open method, which is getting a SQL command along with a parameter values:

 

q := TADQuery.Create(nil);
q.Connection := conn;
try
  q.Open('select id from MyTab where name = :p1 and addr = :p2', 
    [edtName.Text, edtAddr.Text]);
  sId := q.FieldByName('id').AsString;
finally
  q.Free;
end;

 

In second option I will use TADConnection.ExecSQLScalar method:

 

sId := conn.ExecSQLScalar('select id from MyTab where name = :p1 and addr = :p2', 
  [edtName.Text, edtAddr.Text]);

 

Note, that there are similar ExecSQL methods, allowing to set a SQL command text and parameter values in single call. Moreover, if to omit a SQL command text, then a query will remain prepared and will be executed with different parameter values.

 

All that and more you can find in AnyDAC help system.

Add Comments