Click here to Skip to main content
Click here to Skip to main content

Accessing dBase Files

, 1 Jul 2003
Rate this:
Please Sign up or sign in to vote.
How to access a dBase file


This demo project shows you easily how you can access a dBase file with ODBC.

How to create a database DSN

Start -> Administrative Tools -> Data Sources (ODBC) -> User-DSN-> Add

I work with a ODBC - Driver for dBase 5.0. At you can download the ODBC-Jet Driver.

You have to create a DSN with the driver Microsoft dBase Driver (*.dbf), with the name test_dbase and with the right path to the file. Without creating the DSN, the application won't find the dBase file!

Using the code

This code is one part of the project, which is the most interesting !

#include "stdafx.h"
#include "afxdb.h"

CRecordset  rec;
CDatabase   db;

char sz_statement [2048] = "";
char sz_connect [250] = "";

CString s_name;
CString s_age;

//Normally -> ODBC;DSN=DB_Name;UID=UserID;
catch(CDBException *e)
    e->Delete ();    

rec.m_pDatabase = &db;

printf("   Name    |  Age\n");

strcpy(sz_statement,"SELECT * FROM name_list");

    rec.Open(CRecordset::forwardOnly, sz_statement);
        rec.GetFieldValue("NAME", s_name);
        printf("%-10s | ",s_name);
        rec.GetFieldValue("AGE", s_age);
catch (CDBException *e)
    e->Delete ();


The string: "ODBC;DSN=test_dbase;" is the connection string for the database.


Call this member function to initialize a newly constructed CDatabase object. Your database object must be initialized before you can use it to construct a recordset object.

rec.m_pDatabase = &db;

Contains a pointer to the CDatabase object through which the recordset is connected to a data source.

rec.Open(CRecordset::forwardOnly, sz_statement);

You must call this member function to run the query defined by the recordset. Before calling Open, you must construct the recordset object.

rec.GetFieldValue("NAME", s_name);

Call this member function to retrieve field data in the current record. You can look up a field either by name or by index. You can store the field value in a CString object.


Call this member function to make the first record in the next rowset the current record. If you have not implemented bulk row fetching, your recordset has a rowset size of 1, so MoveNext simply moves to the next record.


Call this member function to close the recordset.


Call this member function if you want to disconnect from a data


  • 24.01.2003 - First Version is finished...
  • 15.05.2003 - Second Version => changed some text and changed code
  • 02.07.2003 - Major rewrite


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Austria Austria
No Biography provided

Comments and Discussions

Questionconnect to mdf file PinmemberMahdi Nejadsahebi18-Dec-12 4:55 
Generalmore than one file Pinmembercodestudy4-Dec-06 23:25 
GeneralCDX files do not update PinmemberMcGahanFL26-Sep-06 4:47 
Generalerror in sql set connectatt Pinmemberrasha7615-Sep-04 22:20 
GeneralCataloging the DBASE data source for ODBC PinmemberG. Rusch28-Sep-03 19:40 
Questionhow to dbase PinmemberFiguerres5-May-03 17:34 
AnswerRe: how to dbase PinsussAnonymous8-Jul-03 10:17 
Question? PinsussAnonymous3-May-03 6:50 
AnswerRe: ? PinmemberVladimir Afanasyev18-Jun-03 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150128.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by thomask
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid