Click here to Skip to main content
13,049,814 members (78,138 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 8 May 2013

A set of MFC wrapper classes for ADO - Version 2.20J

, 16 May 2013
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "A set of ADO classes - version 2.20"


This is a small update I made to the excellent set of ADO classes by Carlos Antollini. Though this might be a little less common than .NET classes, MFC is still very much in use and I hope the changes would be useful to someone.


Dated back to 2005, the original classes did not account for 64 bit data and compilers. I have taken the liberty to add support for 64 bit data such as MS-SQL BIGINT. I also have support for 64 bit binary data to 64 int conversion useful in situations where we must read MS-SQL ROWVERSION and similar type fields.

I have also added a handful of utility functions to make common tasks part of the classes. I did however kept the original code in place for the most part and insured to keep backward compatibility.

Of course, all of this is meant to be compiled as a 32 bit application. I might add more for 64 bit applications later, if necessary.

You might also have to get a new TLB for some operating system. It is included in this article and from Microsoft. (msado60_Backcompat_i386.tlb)

Using the code

In this section, I will not repeat what Carlos described in detail in his original post but concentrate on my changes.

Tweaking the header for the host operating system

Depending on the bit size of the host operating system, a different version of ADO is used. Make sure to define this value before including the header if needed:

#define _USE_WINX86_FOLDER_    //Use this when compiling on 64 bit PC
#include <ado2.h>              //ADO wrapper class  

New stuff

First, support 64 bit data:

The GetFieldValue functions returns a value that contains the value of a field.

BOOL GetFieldValue(LPCTSTR lpFieldName, INT64& lValue);
BOOL GetFieldValue(int nIndex, INT64& lValue);
BOOL GetFieldValue(LPCTSTR lpFieldName, UINT64& ulValue);
BOOL GetFieldValue(int nIndex, UINT64& ulValue); 

The SetFieldValue functions sets the value of a field.

BOOL SetFieldValue(int nIndex, INT64 lValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, INT64 lValue);
BOOL SetFieldValue(int nIndex, UINT64 lValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, UINT64 lValue); 

New utility functions

Here are my new utility functions that wraps commonly used tasks.

The CADODatabase class has a set of functions that corresponds to the _ConnectionPtr. To this class, I have added these:

  • bool supIsValidDBPtr - Returns true if the object is initialized and is ready to be used
    • On a new pointer (CADODatabase* pDB), pDB->supIsValidDBPtr() should return false
    • On a new initialized pointer (pDB = new CADODatabase), pDB->supIsValidDBPtr() should return true
  • bool supReopenDB - Tries to close and reopen a database, making a test to insure connectivity with a "SELECT 1" recordset
  • bool supIsDBAccessible - Test to see if the current DB object can be accessed and optionally test with a "SELECT 1" recordset or try reopen

The CADORecordset class has a set of functions that corresponds to the _RecordsetPtr. To this class, I have added these:

  • 64 bit support
  • bool supHasRecord() - Simple way to check if the last query returned any rows
  • bool supOpenHasRecord() - Combine the Open method and the above in a single call
  • __int64 supDbRowVersionTo64(LPCSTR pszField) - Return a 64 bit representation of an SQL Server ROWVERSION field or similar type data

The new CADOTool class has a set of static utility functions that wraps common tasks:

  • bool supInitDatabase - Accept a pointer to a pointer, tries to open the connection and then initialize the host's pointer to an initialized CADODatabase object
  • CADODatabase* pDB = NULL;                               //Declare a new empty pointer
    bool bRc = supInitDatabase(&pDB, szConnectString, 10);  //Try to init  
  • void supCloseDB - Accept a pointer to a pointer and safely closes the database and release resources
    supCloseDB(&pData->m_pAltDB);   //Closes the database if opened and the pointer is valid,
                                    //set the host's pointer to NULL on exit  
  • bool supIsValidDBPtr - Test the passed CADODatabase pointer to see if it is allocated and valid on a best effort
  • bool supIsValidRSPtr - Test the passed CADORecordset pointer to see if it is allocated and valid on a best effort
  • void supCleanCMD, void supCleanRS, and void supCleanPrm all releases appropriate resources and set the host's pointer to NULL.
  • bool adoBuildConnection - Displays the ADO conenction wizard UI and sets the composed connection string in the passed CString if success or return false if cancelled or error.



This sample tries to demonstrate basic usage of the classes with a fictitious database. 

CString csConnect;
CString csSQL;
CADODatabase* pDB = NULL;		//Important, set to NULL!!
CADORecordset* pRS = NULL;		//Important, set to NULL!!
int iUpdated;

__int64 iID;
CString csName;
COleDateTime cdtBirth;
BOOL bActive;

//Build a connect string
	OutputDebugString("You cancelled the dialog!\n");

//Open the database
if(!CADOTool::supInitDatabase(&pDB, csConnect))
	OutputDebugString("Somehow, the database cannot be opened!\n");

//Get some data with this made-up query
csSQL = "SELECT ID, fldName, fldBirth, fldIsActive FROM tblUserData ORDER BY fldMemberDate DESC";	

//Use ADO...

	if(pRS->supOpenHasRecord(csSQL))	//If we have some data
		//We are on the first record
			pRS->GetFieldValue("ID", iID);
			pRS->GetFieldValue("fldName", csName);
			pRS->GetFieldValue("fldBirth", cdtBirth);
			pRS->GetFieldValue("fldIsActive", bActive);

			//Do something with the data

			//Edit something!
			pRS->SetFieldValue("fldIsActive", true);

			pRS->MoveNext();	//Move! Otherwise be caught in an infinite loop!
		//What!? Either cannot open or we don't have data
			//Add new record
			pRS->SetFieldValue("fldName", CString("First, Last"));
			pRS->SetFieldValue("fldBirth", COleDateTime(1980, 10, 2, 0, 0, 0));
			pRS->SetFieldValue("fldIsActive", TRUE);
			pRS->GetFieldValue("ID", iID);	//Get our newly created ID

	//Now, lets try a direct query to our fictitious DB
	if(pDB->Execute("UPDATE tblManager SET fldLastChech = GETDATE() WHERE ID = 3"))
		iUpdated = pDB->GetRecordsAffected();	//Get affected records by last query
catch(CADOException& cEx)
	OutputDebugString("Oups, got a database error!\n");

//No matter what, clean all!
CADOTool::supCleanRS(&pRS);	//Clean resources from our recordset
CADOTool::supCloseDB(&pDB);	//Release our DB


  • May 7 2013 - First release of my contributed code to the original 2.20 
  • May 15th 2013 - Added example and fixed a function error (supOpenHasRecord) 


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Joffrey Brouillet
Chief Technology Officer Somum Solutions Inc.
Canada Canada
In my 22+ years in the business, I had the privilege to work for big name companies in challenging environments in different countries. I created and helped deploy solutions for small businesses as well as for major corporations spanning many offices worldwide. I also have entrepreneurial qualities having had a few companies of my own making me very autonomous and giving me the understanding of the business side of things.

Currently, I play several major roles alongside consulting. I am privileged to be part of the executive team with my employer, answering directly to the owner and where I can put to good use my skills in management, software architecture, partner and supplier relationships development, major account solution engineering and more.

I have strong interrelation skills which facilitate team management and commitment. I promote brainstorms and initiatives from my teams with very positive results. I put forward the human side of a relationship.

You may also be interested in...

Comments and Discussions

QuestionHow to make ado2.cpp work with 64 bit application (x64) Pin
Member 32224617-Dec-13 20:07
memberMember 32224617-Dec-13 20:07 
AnswerRe: How to make ado2.cpp work with 64 bit application (x64) Pin
Joffrey Brouillet18-Dec-13 12:40
professionalJoffrey Brouillet18-Dec-13 12:40 
GeneralRe: How to make ado2.cpp work with 64 bit application (x64) Pin
Member 3222463-Jan-14 9:00
memberMember 3222463-Jan-14 9:00 
QuestionAdd Asynchronous query execution may be good Pin
NewttleLi1-Nov-13 16:52
memberNewttleLi1-Nov-13 16:52 
AnswerRe: Add Asynchronous query execution may be good Pin
Joffrey Brouillet1-Nov-13 17:05
professionalJoffrey Brouillet1-Nov-13 17:05 
GeneralRe: Add Asynchronous query execution may be good Pin
NewttleLi2-Nov-13 3:27
memberNewttleLi2-Nov-13 3:27 
Questionmultithreading ? Pin
Member 1029223823-Sep-13 7:03
memberMember 1029223823-Sep-13 7:03 
AnswerRe: multithreading ? Pin
Joffrey Brouillet23-Sep-13 8:54
professionalJoffrey Brouillet23-Sep-13 8:54 
GeneralRe: multithreading ? Pin
Member 1029223823-Sep-13 19:53
memberMember 1029223823-Sep-13 19:53 
GeneralRe: multithreading ? Pin
Member 1029223823-Sep-13 20:04
memberMember 1029223823-Sep-13 20:04 
Suggestioncompile error ! Pin
jungsuk han7-Aug-13 9:56
memberjungsuk han7-Aug-13 9:56 
Questioncompile error on VS2012 Pin
tairan20061-Aug-13 21:30
membertairan20061-Aug-13 21:30 
AnswerRe: compile error on VS2012 Pin
Joffrey Brouillet23-Sep-13 8:55
professionalJoffrey Brouillet23-Sep-13 8:55 
AnswerRe: compile error on VS2012 Pin
Theo Buys1-Apr-15 5:25
memberTheo Buys1-Apr-15 5:25 
GeneralMy vote of 5 Pin
Mihai MOGA13-Jun-13 20:44
memberMihai MOGA13-Jun-13 20:44 
GeneralRe: My vote of 5 Pin
Joffrey Brouillet23-Sep-13 8:57
professionalJoffrey Brouillet23-Sep-13 8:57 
QuestionWindows 7 SP1 Pin
viaducting16-May-13 2:47
memberviaducting16-May-13 2:47 
AnswerRe: Windows 7 SP1 Pin
Joffrey Brouillet16-May-13 4:10
professionalJoffrey Brouillet16-May-13 4:10 
Questiongreat job.if there is a demo application will be much better Pin
ASERERTA@#@s10-May-13 18:11
memberASERERTA@#@s10-May-13 18:11 
great job.if there is a demo application will be much better
AnswerRe: great job.if there is a demo application will be much better Pin
Joffrey Brouillet16-May-13 4:12
professionalJoffrey Brouillet16-May-13 4:12 
GeneralRe: great job.if there is a demo application will be much better Pin
ASERERTA@#@s16-May-13 4:43
memberASERERTA@#@s16-May-13 4:43 
GeneralMy vote of 5 Pin
kevinwzy8-May-13 6:11
memberkevinwzy8-May-13 6:11 
GeneralRe: My vote of 5 Pin
Joffrey Brouillet8-May-13 7:08
memberJoffrey Brouillet8-May-13 7:08 

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
Web02 | 2.8.170713.1 | Last Updated 16 May 2013
Article Copyright 2013 by Joffrey Brouillet
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid