Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
488 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 (0)


Export ALL tables from MS jet to CSV via ADOGo to John Pears's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 4.x (or higher)
Category:
DB-General
Skill Level:
Scoring:
Last Update:
09/19/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet TADOConnection TADOTable Access mdb
Times Scored:
2
Visits:
4341
Uploader: John Pears
Company: coventry Uni ( a technician )
Reference: N/A
 
Question/Problem/Abstract:
how to export All Tables in a Microsoft Jet DB to a CSV file
Answer:





procedure TMainForm.SaveAllTablesToCSV(DBFileName:string);
var
   InfoStr,
   FileName,
   RecString,
   WorkingDirectory:string;
   OutFileList,
   TableNameList:TStringList;
   TableNum,
   FieldNum:integer;
   VT:TVarType;
begin
  ADOTable1.Active := false;
  WorkingDirectory := ExtractFileDir(DBFileName);
  TableNameList := TStringList.Create;
  OutFileList := TStringList.Create;
  InfoStr := 'The following files were created' + #13#13;

  ADOConnection1.GetTableNames(TableNameList,false);
  for TableNum := 0 to TableNameList.Count - 1 do
  begin
    FileName := WorkingDirectory + '\' +
                TableNameList.Strings[TableNum] + '.CSV';
    Caption := 'Saving "' + ExtractFileName(FileName) + '"';
    ADOTable1.TableName := TableNameList.Strings[TableNum];
    ADOTable1.Active := true;
    OutFileList.Clear;

    ADOTable1.First;
    while not ADOTable1.Eof do
    begin

      RecString := '';
      for FieldNum := 0 to ADOTable1.FieldCount - 1 do
      begin
        VT := VarType(ADOTable1.Fields[FieldNum].Value);
        case VT of
           // just write the field if not a string
           vtInteger, vtExtended, vtCurrency, vtInt64:
             RecString := RecString + ADOTable1.Fields[FieldNum].AsString
        else
           // it IS a string so put quotes around it
           RecString := RecString + '"' +
                        ADOTable1.Fields[FieldNum].AsString + '"';
        end; { case }

        // if not the last field then use a field separator
        if FieldNum < ( ADOTable1.FieldCount - 1 ) then
          RecString := RecString + ',';
      end; { for FieldNum }
      OutFileList.Add(RecString);

      ADOTable1.Next;
    end; { while }

    OutFileList.SaveToFile(FileName);
    InfoStr := InfoStr + FileName + #13;
    ADOTable1.Active := false;

  end; { for  TableNum }
  TableNameList.Free;
  OutFileList.Free;
  Caption := 'Done';
  ShowMessage(InfoStr);
end;


procedure TMainForm.Button1Click(Sender: TObject);
const
  ConnStrA = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=';
  ConnStrC = ';Persist Security Info=False';
  ProvStr = 'Microsoft.Jet.OLEDB.4.0';
begin
  OpenDialog1.InitialDir := ExtractFileDir(ParamStr(0));
  if OpenDialog1.Execute then

    try
      ADOConnection1.ConnectionString :=
        ConnStrA + OpenDialog1.FileName + ConnStrC;
      ADOConnection1.Provider := ProvStr;
      ADOConnection1.Connected := true;
      ADOTable1.Connection := ADOConnection1;
      SaveAllTablesToCSV(OpenDialog1.FileName);
    except
       ShowMessage('Could not Connect to ' + #13 +
                 '"' +  OpenDialog1.FileName + '"');
       Close;
    end;

end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Carter
 
   














 







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