Click here to Skip to main content
13,147,956 members (51,063 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 2 May 2003

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 !

<PRE lang=c++>#include "stdafx.h" #include "afxdb.h" CRecordset rec; CDatabase db; char sz_statement [2048] = ""; char sz_connect [250] = ""; CString s_name; CString s_age; sprintf(sz_connect,"ODBC;DSN=test_dbase;"); //Normally -> ODBC;DSN=DB_Name;UID=UserID; //PWD=Password;DBALIAS=DB_Alias; try { db.Open(sz_connect); } catch(CDBException *e) { e->Delete (); } rec.m_pDatabase = &db; printf(" Name | Age\n"); printf("------------------\n"); strcpy(sz_statement,"SELECT * FROM name_list"); try { rec.Open(CRecordset::forwardOnly, sz_statement); while(!rec.IsEOF()) { rec.GetFieldValue("NAME", s_name); printf("%-10s | ",s_name); rec.GetFieldValue("AGE", s_age); printf("%-5s\n",s_age); rec.MoveNext(); } rec.Close(); } catch (CDBException *e) { e->Delete (); } db.Close();

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

You may also be interested in...

Comments and Discussions

Questionconnect to mdf file Pin
Mahdi Nejadsahebi18-Dec-12 3:55
memberMahdi Nejadsahebi18-Dec-12 3:55 
Generalmore than one file Pin
codestudy4-Dec-06 22:25
membercodestudy4-Dec-06 22:25 
GeneralCDX files do not update Pin
McGahanFL26-Sep-06 3:47
memberMcGahanFL26-Sep-06 3:47 
Generalerror in sql set connectatt Pin
rasha7615-Sep-04 21:20
memberrasha7615-Sep-04 21:20 
GeneralCataloging the DBASE data source for ODBC Pin
G. Rusch28-Sep-03 18:40
memberG. Rusch28-Sep-03 18:40 
Questionhow to dbase Pin
Figuerres5-May-03 16:34
memberFiguerres5-May-03 16:34 
AnswerRe: how to dbase Pin
Anonymous8-Jul-03 9:17
sussAnonymous8-Jul-03 9:17 
Question? Pin
Anonymous3-May-03 5:50
sussAnonymous3-May-03 5:50 
AnswerRe: ? Pin
Vladimir Afanasyev18-Jun-03 20:46
memberVladimir Afanasyev18-Jun-03 20:46 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by thomask
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid