Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

Accessing dBase Files

Rate me:
Please Sign up or sign in to vote.
1.79/5 (27 votes)
1 Jul 20032 min read 102.4K   2.5K   26   9
How to access a dBase file

Introduction

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 microsoft.com 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 !

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.

db.Open(sz_connect);

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.

rec.MoveNext();

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.

rec.Close();

Call this member function to close the recordset.

db.Close();

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

History

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

License

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


Written By
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionconnect to mdf file Pin
Mahdi Nejadsahebi18-Dec-12 3:55
Mahdi Nejadsahebi18-Dec-12 3:55 
Generalmore than one file Pin
codestudy4-Dec-06 22:25
codestudy4-Dec-06 22:25 
GeneralCDX files do not update Pin
mcgahanfl26-Sep-06 3:47
mcgahanfl26-Sep-06 3:47 
Generalerror in sql set connectatt Pin
rasha7615-Sep-04 21:20
rasha7615-Sep-04 21:20 
GeneralCataloging the DBASE data source for ODBC Pin
G. Rusch28-Sep-03 18:40
G. Rusch28-Sep-03 18:40 
Questionhow to dbase Pin
Figuerres5-May-03 16:34
Figuerres5-May-03 16:34 
AnswerRe: how to dbase Pin
Anonymous8-Jul-03 9:17
Anonymous8-Jul-03 9:17 
Question? Pin
Anonymous3-May-03 5:50
Anonymous3-May-03 5:50 
AnswerRe: ? Pin
Vladimir Afanasyev18-Jun-03 20:46
Vladimir 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.