Old Trick (Part 2): Retrieving DBF from Borland Delphi

Couple months ago, I'd wrote about how to retrieving MDB database (MS Access) from Borland Delphi. The same thing if we need to connect to an old-fashioned DBF from Delphi application - the different is: it's more quite simpler.

Here below is an example how to read records from a FILENAME.DBF (containing 2 fields; FIELD1 and FIELD2). Don't forget to provide the DBF file on the same directory where the project will be saved - a CDX index file required depends on the DBF typically records. Reading DBF records as simply as using SQL SELECT query just like usual.

To create this basic project, all we need is only TQuery and TButton. Just put it both on a blank form.



The TQuery will work with DBF in a native way. Here is the code, take an attention on a bold sign below.

procedure TFMain.TButtonClick(Sender: TObject);
var FIELD1,FIELD2 : string;
begin
qDBF1.Close;
qDBF1.DatabaseName:=extractfilepath(extractfilepath(application.ExeName)+'FILENAME.DBF');
qDBF1.SQL.Clear;
qDBF1.SQL.Add('select * from FILENAME');
qDBF1.Open;

qDBF1.First;
repeat
FIELD1:=qDBF1.FieldByName('FIELD1').AsString;
FIELD2:=qDBF1.FieldByName('FIELD2').AsString;

qDBF1.Next;
until qDBF1.Eof;
end;


Anyway, I supposed that manipulating DBF record using TQuery component will have the same way by using INSERT, UPDATE and DELETE clauses. In other words, "Just like usual…" :) Cheers!!!

Labels: ,


PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment

 

Post a Comment

Leave comments here...