How to read and write dbf in a native way?

eKek0 picture eKek0 · Sep 26, 2010 · Viewed 18.1k times · Source

In Delphi for Win32, how to read and write a dbf file in a native way, without the BDE? I know there are some components available in the web, but I have never used any of them, so I don't know which to choose (if any).

Answer

RRUZ picture RRUZ · Sep 26, 2010

You can use ADO to access a DBF File

See ths sample code (using an TAdoConnection and TAdoDataSet components).

var
dbf_folder : string;
begin
  dbf_folder:='c:\bdd';//set your dbf folder location here 
  ADOConnection1.LoginPrompt:=false;
  ADOConnection1.ConnectionString:=Format('Provider=Microsoft.JET.OLEDB.4.0;Data Source=%s;Extended Properties=dBase IV;',[dbf_folder]);
  try
  ADOConnection1.Connected:=True;
  ADODataSet1.CommandText:='Select * from file.dbf'; //make your SQL query using the name of the dbf file
  ADODataSet1.Open;
   while not ADODataSet1.eof do
   begin
   //do your stuff here
   //ADODataSet1.FieldByName('').AsString
   ADODataSet1.Next;
   end;
  except
    on E : Exception do
      ShowMessage(E.Message);
  end;
end;