ODBC MFC Classes: Accessing Data at Runtime

Submitted by date of submission user level 

Mahesh Chand OCt 25, 2000 Beginner 

You can access databases using MFC ODBC classes without help of MFC AppWizard and ClassWizard. This article explains how to access data using CDatabase and CRecordset classes at runtime. Here are few simple steps:

1. Connect to the database 

CDatabase and CRecordset classes are used to create and open database and recorsets respectively. CDatabase::Open method creates a connection to the database. First parameter of CDatabase::Open() is ODBC Data Source Name (DSN). You can create DSN by using ODBC Admin from your control panel. Say you create a DSN name mcbDSN using some database "C:\mcb.mdb".

CDatabase m_db;

if ( !m_db.Open( "mcbDSN", FALSE, FALSE, "ODBC;", FALSE ) )

  return ; 

2. Connect to the table

CRecordset class is used to create a connection to a table in the database. You pass CDatabase pointer in the constructor of CRecordset to connect CRecordset to CDatabase. First parameter of CRecordset::Open() is record set type. It can be snapshot, dynaset, or forwardonly. Second parameter is SQL query. You select your table name(s), columns in this query. I  am using 'myTable' table to read data from. 

CRecordset recSet(&m_db);

recSet.Open(CRecordset::dynaset, "SELECT * From myTable", CRecordset::none ); 

3. Get Data

Use GetFieldValue of CRecordset to get value of a column and use GetODBCFieldCount to get total number of columns in a table. Read data until you reached EOF.

recSet.GetFieldValue(iIndex, var); 

4. Close connections

CDatabase::Close() and CRecordset::Close() close these connections.

recSet.Close(); 

m_db.Close(); 

Sample Code:

CString strItems;

int iIndex, nFields, iSubItem = 1;

CString *szVal;

double dVal;

short nVal;

long lVal;

TIMESTAMP_STRUCT *date;

BOOL bVal;

CDatabase m_db;

if ( !m_db.Open( "mcbDSN", FALSE, FALSE, "ODBC;", FALSE ) )

  return ;

CRecordset recSet(&m_db);

recSet.Open(CRecordset::dynaset, "SELECT * From myTable", CRecordset::none );

CDBVariant var;

nFields = recSet.GetODBCFieldCount();

recSet.MoveFirst();

while(!recSet.IsEOF())

{  

for(iIndex = 0; iIndex < nFields; iIndex++)

     recSet.GetFieldValue(iIndex, var);

     switch(var.m_dwType)

     {

     // File type is TEXT

     case DBVT_STRING:

     szVal = var.m_pstring;

     break; 

      // File type is DOUBLE

     case DBVT_DOUBLE:

     dVal = var.m_dblVal;

     break; 

   // File type is BOOL

   case DBVT_BOOL:

   bVal = var.m_boolVal;

   break;

   // File type is LONG

   case DBVT_LONG:

   lVal = var.m_lVal;

   break; 

   // File type is SHORT

   case DBVT_SHORT:

   nVal = var.m_iVal;

   break; 

   // File type is DATE

   case DBVT_DATE:

   date = var.m_pdate;

   break; 

   default:

   break;

  } 

recSet.MoveNext(); 

 

 

 

 

recSet.Close(); 

m_db.Close(); 

 

Did you like this article? Please send me your feedback on Mahesh. Your feedback helps me to improve the quality of my next article.

About the Author:

Mahesh is Admin and the founder of this site. He has been programming in C++, MFC, Visual Basic, COM, ATL, Database Programming over 4 years. He can be reached on Mahesh. His  background includes Master's in Computer Science and Batchelor's in Mathematics and Physics. See members for more details.