delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
493 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Save DataSet As CSV TextfileGo to Kjetil Rodal's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
DB-General
Skill Level:
Scoring:
Last Update:
10/19/2005
Search Keys:
delphi delphi3000 article borland vcl code-snippet DB DataSet CSV Textfile File Save-As-TextFile
Times Scored:
6
Visits:
5554
Uploader: Kjetil Rodal
Company: Select NOR
Reference: Kjetil Rodal
 
Question/Problem/Abstract:
How to save the content of a DataSet as a CSV textfile?
Answer:



Easy and quick way to save a dataset as a CSV file. Works with both BDE and DBExpress datasets.
Tip: if you have the data displayed in a grid, set the DBGrid1.Enabled to false before calling the SaveAsCSV procedure, otherwise the procedure will be as slow as your screen display...

*************************************************************
procedure btnSaveClick(Sender: TObject);
begin
  DBGrid1.Visible := False;
  try
    if (SaveDialog1.Execute = true) and
     (length(SaveDialog1.FileName) > 0) then
      begin
        SaveDialog1.DefaultExt := 'CSV';
        SaveAsCSV(SaveDialog1.FileName,ClientDataSet1);
      end;
  finally
    DBGrid1.Visible := True;
  end;  
end;
*************************************************************
procedure SaveAsCSV(myFileName: string; myDataSet: TDataSet);
var
  myTextFile: TextFile;
  i: integer;
  s: string;
begin
  //create a new file
  AssignFile(myTextFile, myFileName);
  Rewrite(myTextFile);

  s := ''; //initialize empty string

  try
    //write field names (as column headers)
    for i := 0 to myDataSet.FieldCount - 1 do
      begin
        s := s + Format('"%s";', [myDataSet.Fields[i].FieldName]);
      end;
    Writeln(myTextFile, s);

    //write field values
    while not myDataSet.Eof do
      begin
        s := '';
        for i := 0 to myDataSet.FieldCount - 1 do
          begin
            s := s + Format('"%s";', [myDataSet.Fields[i].AsString]);
          end;
        Writeln(myTextfile, s);
        myDataSet.Next;
      end;

  finally
    CloseFile(myTextFile);
  end;
end;
*************************************************************






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
slow
    danny brr (Feb 3 2005 4:58PM)

easy way but very slow for a big dataset!
Respond

RE: slow
Kjetil Rodal (Mar 15 2005 12:14PM)

I haven't noticed that it's specially slow - but make sure you disable DBGrid or such so that you don't have to wait for the screen to scroll through the dataset together with the saving procedure.. if it's still slow, give a hint...
Respond

RE: RE: slow
Bogdan Badulescu (Aug 1 2007 8:12PM)

This is probably the slowest and error-prone way to backup a dataset. Especially if you try to restore that data on a system that has a different locale.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)