Click here to Skip to main content
13,554,422 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 16 Jun 2006
Licenced CPOL

Using ADO to connect to a Visual FoxPro (dbf) table in C++

, 16 Jun 2006
Rate this:
Please Sign up or sign in to vote.
This is a simple sample showing how to get a database connection using ADO to connect a dbf table.


This snippet shows how to connect a Visual Foxpro dbf table and get the field names of the table. It comes from another application in which there should be a database connection interface. And the following is the test function for the interface and it works.

As we know dbf tables are stored independently in some file in the disk, not like a .mdb database whose tables can not be seen at the location the database is via Explorer. So when connecting, the file storing the dbf can be regarded as a database. The function below implements the connection:

void CTttView::OnTest() 
   _ConnectionPtr m_pCon;
   _RecordsetPtr m_pRecordset;



   HRESULT hr;
      //The SourceDB should be a proper directory
      hr=m_pCon->Open("Driver={Microsoft Visual FoxPro Driver}; SourceType=DBF; "
                      "SourceDB=E:\\c program;", "","",adModeUnknown); 
   catch(_com_error e)
      AfxMessageBox("Connection failed, check the direction!");

   m_pRecordset->Open("SELECT * FROM Table1", //Table1 is a dbf table name
   m_pCon.GetInterfacePtr(),  adOpenDynamic, adLockOptimistic, adCmdText);

   FieldsPtr pFD=m_pRecordset->GetFields();

   _variant_t index;
   index.vt = VT_I2;
   for(int i=0;i<(int)pFD->GetCount();i++)
      CString str;
      str.Format("Field: %s" ,(LPCSTR)pFD->GetItem(index)->GetName());



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
China China
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralFinding the Columns in an Index Pin
AlexEvans12-Oct-06 21:28
memberAlexEvans12-Oct-06 21:28 

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
Web03-2016 | 2.8.180515.1 | Last Updated 17 Jun 2006
Article Copyright 2006 by zhipeng0117
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid