CDbaseFile

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;
                     rc=dBaseFile.GetNextRecord())
  {
   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
  dBaseFile.Close();
}


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
fieldsArray.Add("NAME,C,50,0");
// create NUMERIC field
fieldsArray.Add("CLIENTID,N,10,0");
// create DATE field
fieldsArray.Add("BIRTHDAY,D,8,0");
// create LOGICAL field
fieldsArray.Add("SUBSCRIPTION,L,1,0");
// create FLOAT field (length: 8, decimals: 2)
fieldsArray.Add("PAYMENT,F,8,2");

CDbaseFile dBaseFile;

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


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



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



Delete (current) record::
// delete second record
dBaseFile.GetRecord(2);
dBaseFile.DeleteRecord();

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:

dBaseFile.Pack();


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
dBaseFile.GetRecord(7);

// get memo field length
DWORD dwLength = dBaseFile.GetMemoFieldLength("NOTES");
if (dwLength)
{
  try
  {
   // 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;
  }
  catch(...)
  {
  }
}


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


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.