Querying Data
You can use the TFDQuery class to build and execute SELECT statements at design time from the Query Editor as well as at run time.
Using TFDQuery from the Design Editor
You can use the design editor to build SQL queries: After you drop a TFDQuery instance onto a form, the Connection property is automatically set to a TFDConnection object on the form, if one is defined. You can then double-click the instance to open the FireDAC Query Editor. In this dialog you can write SQL queries and preview the results.
Using TFDQuery from Code
Use the Open method of the TFDQuery class to execute SELECT statements, as this overloaded method returns a result set. Before executing the query, the Connected property of the TFDConnection object must be set to true:
FDConnection1.Connected := true;
FDQuery1.Open('SELECT * FROM Customer WHERE Name = ''CData, Inc.''');
Note that TFDQuery objects use prepared statements by default.
If you need to execute a command only once, set the ResourceOptions.DirectExecute property of the TFDConnection object to true.
Iterating over Results
You can access fields in the result like so; for example, to populate a TMemo object:
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
try
FDQuery1.Open();
while not FDQuery1.Eof do
begin
for I := 0 to FDQuery1.Fields.Count - 1 do
begin
Memo1.Lines.Add(FDQuery1.Fields[i].FieldName);
Memo1.Lines.Add(FDQuery1.Fields[i].AsString);
Memo1.Lines.Add(IntToStr(FDQuery1.Fields[i].DataSize));
end;
FDQuery1.Next;
end;
finally
FDQuery1.Close;
FDQuery1.Free;
end;
end;