user10053091
user10053091

Reputation:

How to print data of _RecordSet to the text file in delphi 10

how to print all data/records contain in Recordset to the text file in Delphi 10 ? I could not able to find any Method or property for it.Please guide, i am newbie in delphi.

I have done following:

Var:
    CurrField : Field;
    RecSet:_RecordSet ;
Begin:
    RecSet:= command.Execute(records,Params,-1);
    CurrField := RecSet.Fields[0];
end;

but i want to print complete records/data contain in RecSet(_RecordSet type) in text file.

Upvotes: 0

Views: 1227

Answers (1)

Ken White
Ken White

Reputation: 125749

You can write it yourself. If the recordset is relatively small, the easiest way is to use a TStringList.

var
  i: Integer;
  s: string;
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    while not RecSet.Eof do
    begin
      // Clear string for the next row
      s := '';
      // Loop through the fields in this row, creating a comma-separated list
      for i := 0 to RecSet.FieldCount - 1 do
        s := s + RecSet.Fields[i].Value + ',';
      // Remove unnecessary final comma at end
      SetLength(s, Length(s) - 1); 
      // Add to the stringlist
      SL.Add(s);
    end;
    // Save the stringlist content to disk
    SL.SaveToFile('YourFileName.txt');
  finally
    SL.Free;
  end;
end;

Upvotes: 2

Related Questions