Mega Code Archive

 
Categories / Delphi / ADO Database
 

ADO Dataset CSV file

Title: ADO Dataset - CSV file Question: how to export a ADO Dataset to a Comma Separated Values file with a few lines of code. Answer: procedure TMainForm.SaveDataSetCSV; const adClipString = 2; ColumnDelimiter = ';'; RowDelimiter = #13#10; var s : String; fs : TFileStream; begin SaveDialog1.DefaultExt := '.CSV'; // build the default filename SaveDialog1.FileName := ADOTable1.TableName+SaveDialog1.DefaultExt; if SaveDialog1.Execute then begin // Get the complete dataset as CSV // the underlying Recordset-Object does the whole work for you s := ADOTable1.Recordset.GetString(adClipString, ADOTable1.Recordset.RecordCount, ColumnDelimiter, RowDelimiter, ''); // save the CSV string to a file fs := TFileStream.Create(SaveDialog1.FileName, fmCreate); try fs.WriteBuffer(s[1], Length(s)); finally fs.Free; end; end; end;