CDbaseFile - Read/Write DBASE files.
A few years ago I developed a dBase III compatible database class as an alternative to the codebase libraries.
The code is partially based on Turbo C source code (created in 1987!) by Mark Sadler, but has been totally rewritten to be compatible with 32 bits and lots of functionality has been added.
Although I can’t image someone is still using dBase databases, I decided to publish the class to this website.
CDbaseFile does not require external drivers for connection to databases (ODBC, BDE).

The following examples give you an idea of how to use the class:

Open an existing database and show all records:
// Open database
CDbaseFile dBaseFile;

if (dBaseFile.Open("DATABASE.DBF") == DBASE_SUCCESS)
  // show all records
  for(int rc=dBaseFile.GetFirstRecord(); rc==DBASE_SUCCESS;
   CString strName = m_database.GetCharField("NAME");
   CString strAddress = m_database.GetCharField("ADDRESS");
   long nClientID = m_database.GetNumericField("CLIENTID");
   TRACE3("Record data: %s, %s, %d\n.", strName, strAddress, nClientID);
  // close database

Instead of GetCharField(), you can also use GetField() which returns the record data in a string buffer whatever the type of the field is, for example:

char szBuff[255];
m_database.GetField("NAME", szBuff);

Create a new database:
// define the database structure in a string array
CStringArray fieldsArray;

// create CHAR field
// create NUMERIC field
// create DATE field
// create LOGICAL field
// create FLOAT field (length: 8, decimals: 2)

CDbaseFile dBaseFile;

// create empty database.dbf
if (dBaseFile.Create("DATABASE.DBF", fieldsArray) == DBASE_SUCCESS)
  // successfull created database

Add record::
// add new record
// put some data in the record
dBaseFile.PutCharField("NAME", "Pablo van der Meer");
dBaseFile.PutNumericField("CLIENTID", 123456);
// save record

Edit existing record:
// get 7th record
// put some data in the record
dBaseFile.PutCharField("NAME", "Pablo van der Meer");
dBaseFile.PutNumericField("CLIENTID", 123456);
// save record

Delete (current) record::
// delete second record

When you delete a record it is not really removed from the database, but only marked as deleted.
To remove deleted records from the database use:


How to use memo fields:
When there are memo fields present in a database, the class will automatically open the matching *.dbt file.
The following code shows how to read a memo field:

char *buff;
CString strNotes;

// get 7th record

// get memo field length
DWORD dwLength = dBaseFile.GetMemoFieldLength("NOTES");
if (dwLength)
   // allocate memory for field contents
   buff = new char[dwLength+1];
   // get memo field data
   dBaseFile.GetMemoField("NOTES", buff, dwLength);
   // put data in strNotes
   strNotes.Format("%s", buff);
   // clear up
   delete buff;

Update memo field:
// get second record
// put data in memofield (length = 17)
dBaseFile.PutMemoField("NOTES", "This is a message", 17);
// update second record

Check out the Address Book example to get an impression of how to use the class in a 'real life' application.

Download demo executable

Download source code
This class is part of the Pablo Software Solutions MFC Extension Package - Classes Edition

[Home] [Products] [Source Code] [Downloads] [Support]

© 2015 - Pablo Software Solutions
All rights reserved.