Search code examples
sqldelphivclfiredac

How to get FDQuery result?


Hi guys I have a few forms in my application and I have a test button where I want to fire a query taken from another unit. Here is my code:

procedure TForm1.Button7Click(Sender: TObject);
var test : string;
begin
// test :=  Unit1.DataModule1.FDQuery2.Open.ToString;                  
 // Unit1.DataModule1.FDQuery2.ExecSQL;
 // test.ToString;
 // ShowMessage(Unit1.DataModule1.FDQuery2.Open);
 // Unit1.DataModule1.FDQuery2.Active := true;
  Unit1.DataModule1.FDQuery2.SQL.Text := 'SELECT CURRENT_DATE';
  Unit1.DataModule1.FDQuery2.Open();
  // Writeln(test);
end;

basically I want to be able to for example take the result array or whatever datatype returns and assign them for example to a TEdit and then change it and so on. I wanted to show the result as a string but could not convert it properly. How would you the store the response? Also please correct my calls if there is something wrong - because my TFDQuery2 has the same argument like SQL.TEXT. Is it possible to just fire that query and get the result in a way that I can show it or use it?


Solution

  • After TFDQuery.Open() has executed the SELECT query, you can use the TFDQuery.Fields property to access the returned field values, eg:

    procedure TForm1.Button7Click(Sender: TObject);
    var
      test : string;
    begin
      Unit1.DataModule1.FDQuery2.SQL.Text := 'SELECT CURRENT_DATE';
      Unit1.DataModule1.FDQuery2.Open();
      test := Unit1.DataModule1.FDQuery2.Fields[0].AsString;
      Edit1.Text := test;
      Unit1.DataModule1.FDQuery2.Close();
    end;