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).
7 Answers
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;

- 134,889
- 20
- 356
- 483
I used TDBF back when I was still working with DBF files (some legacy apps). I still use it for maintainance of those apps here and there. It is free, has a lot of features and works good.

- 6,073
- 26
- 38
-
"free" meaning LGPL in this case. Not good for commercial apps. – dummzeuch Aug 20 '11 at 11:25
-
Why wouldn't it be good for commercial apps? You only have to publish TDBF source if you modify it; otherwise you can just link in the code.. – reiniero Mar 26 '13 at 07:58
-
1If you *statically* link it you have to provide not only the tdbf source code but the full source of your program. In order to dynamically link tdbf you'd have to work with packages. – dummzeuch Jun 30 '13 at 17:22
I used Topaz from Software Science for many years before I got started with Firebird. It was always an excellent library, had a terrific manual and good technical support. It supports indexes and even has an in-memory option. I think it would be a good choice.

- 2,237
- 1
- 21
- 31
-
Unfortunately I found out recently (after I had already spent too many hours on converting code) that it does not support NULL values. They automatically get converted to empty strings or 0 depending on the field type. This missing feature made Topaz completely worthless for me. :-( Now I have applied for their "60 days money back" offer. I hope they stick to it so at least I get those 100 US$ back. The time is already lost. – dummzeuch Aug 20 '11 at 11:23
-
I'm really sorry it was not suitable. They were a very professional client oriented company back when I dealt with them on a regular basis. I hope your experience is as good as mine was. Again, I am sorry that I pointed you down a blind alley. – jrodenhi Aug 22 '11 at 20:46
-
jrodenhi: That certainly wasn't your fault. I only found your answer after I had already determined that Topaz did not do what I needed. I posted this comment to make others aware of this shortcoming so they don't waste as much time on it as I did. – dummzeuch Aug 23 '11 at 07:36

- 2,476
- 5
- 36
- 62
-
1I used this one, but migrated to TDBF, mainly because of speed reasons. Despite having bought Apollo – Marco van de Voort Sep 27 '10 at 12:29
ADO didn't work for me but I managed to open my dbf file using BDE:
From a Data Access (or BDE, depends on your version of Delphi) section I put a TDataBase and, TTable components (you can use TQuery if you want).
By doubleclick on TDataBase component I opened the setup dialog. Filled the Name field with 'db_name' (the name is arbitrary), driver name = 'STANDARD', Parameters field: 'PATH=C:\Path\To\DBF_FILES\'. Then I set Connected=True.
Then in TTable component I set DatabaseName = 'db_name' - the one I set in TDataBase component. And TableName property set 'DB_FILE.dbf' which was located in the specified folder. Active = True.
You know what to do next

- 4,521
- 1
- 15
- 32
It is not hard to read a DBF file if you don't need indexes. The format is pretty straightforward. A header followed for fixed sized registers. There is a flag in each register which indicates if it is deleted or not. I suggest looking for a component which does what you want. You can find some in Torry's Delphi pages.

- 1,515
- 1
- 26
- 38
You can try this simple way
Query1.Close;
Query1.DatabaseName := ExtractFilePath(Application.ExeName); //path to your file
Query1.SQL.Text := 'SELECT * FROM Test_123.dbf';
Query1.Open;
-
Is Query1 a TQuery? In that case it will only work if using the BDE which he doesn't want to do. If Query1 is not a TQuery, what is it? – dummzeuch Mar 14 '23 at 11:01