Click here to Skip to main content
13,350,486 members (66,802 online)
Click here to Skip to main content
Add your own
alternative version


96 bookmarked
Posted 21 Dec 2007

dbAx: a C++ Library for ActiveX Data Objects (ADO)

, 21 Jan 2009
Rate this:
Please Sign up or sign in to vote.
C++ class wrapper for ADO
Screenshot - dbAx.jpg


I have long worked with database design and implementation, primarily with Microsoft SQL Server. I am also a die-hard C++ fan. The old school gave us Data Access Objects (DAO), which provided a fairly good interface for C++ users. Then ActiveX Data Objects (ADO) came along. Great environment, but not a lot of support for the C++ crowd. I began to feel like the red-headed orphan stepchild.

As I said, I live, eat and sleep C++. I am also passionate about document management (another subject altogether) and interfacing with many other applications and libraries, most of which involve getting to a database of some sort. I know, I know. There’s VB: don’t want to go there. There’s also .NET: haven’t got (all the way) there yet!

To get to the point: this inspired me to work on my own set of classes that would make database-ing not so painful for the C’er in all of us. Rather than coming up with some other variation involving an "A" and a "D" and an "O," I chose the name dbAx.


Actually, dbAx is a library composed of C++ classes collectively called AxLib and a small utility program called AxGen. AxLib contains classes for connecting to a data source and command, as well as recordset classes for dealing with the actual data tables. The AxGen utility allows you to easily create custom versions of the command and recordset classes for the task at hand.

Some of the benefits to using dbAx are freedom from dealing with COM interfaces and pointers, easily implementing parameterized queries (stored procedures) and avoiding the manipulation of variant data types. The library automatically handles the translation to and from native C++ class data members.

Before continuing, it should be noted that included in the available download is a Windows compiled help file that covers all aspects of dbAx. I will only mention the main points here. Also, there is a sample application called CardFile that demonstrates many of the features of the dbAx library.

Using the Code

AxLib defines six classes, several of which have virtual methods and are intended to act as a base class for an application-specific version. The primary classes are: CAxConnection, CAxCommand and CAxRecordset. As you may have guessed, these support the connection to, and accessing of, database information.

In support of these main classes are CAxException, CAxConnectionEvents and CAxRecordsetEvents. The CAxException class provides for exception handling while the CAxConnectionEvents and CAxRecordsetEvents classes enable the handling of events that may be raised by the data provider.

The following is a quick outline of how to get started. Again, the included help file goes into much more detail.

  1. Add all of the files included in AxLib to your project. That is: AxLib.h, AxConnection.cpp, AxCommand.cpp, AxRecordset.cpp and AxException.cpp.
  2. Create derived version(s) of CAxCommand (if stored procedures are involved) and implement the virtual methods _CreateParameters and _UpdateParameters.
  3. Create derived version(s) of CAxRecordset and implement the virtual methods _SetDefaultValues and DoFieldExchange.
  4. Create instances (member variables) of CAxConnection and of the derived versions of CAxCommand and CAxRecordset.

In your application, generally the main procedure will:

  • Call the dbAx::Init() method to initialize the library.
  • Set up a connection string that will be used with the CAxConnection object.
  • Set up and initialize each of the CAxCommand objects, if any.
  • Open the CAxRecordset objects, either directly or by way of an associated CAxCommand object.
  • At program termination, call the dbAx::Term() method.


As noted, the CAxCommand and CAxRecordset classes define virtual methods that are generally overridden and include calls to other class methods for creating and updating parameters, as well as exchanging the field data of a recordset. This involves mapping the database fields to member variables and indicating the correct SQL data type. This can become quite tedious and error-prone, especially in the case of tables with a large number of data fields.

AxGen is a utility that attempts to connect to a target data provider and read the schema of stored procedures and data tables. You can then specify a name and quickly generate the source code for a custom derived class. This can be a big time-saver, especially during development when the database structure may be in a state of constant change. AxGen also implements Microsoft’s connection wizard, where you can build the required connection string for a specific data provider. A sample CAxRecordset class created by AxGen follows:

    File: AxAccountSet.hpp
    Date: 11/22/2007
      By: Data Management Systems (

    The following source code was generated using the AxGen utility and is
    intended to be used in conjunction with the dbAx library. This class
    facilitates the exchange of data with the ADO data source from which
    it was derived.

    Table: (local)\CardFile\ACCOUNT

    Include this file in your project.

    This source code is provided AS-IS with no warranty as to its
    suitability or usefulness in any application in which it may be used.

#pragma once
#include <AxLib.h>

using namespace dbAx;

class CAxAccountSet :
    public CAxRecordset
  CAxAccountSet() { _SetDefaultValues(); }
  ~CAxAccountSet() { }

  CString     m_szAccountID;
  CString     m_szName;
  CString     m_szAddress;
  CString     m_szPhone1;
  CString     m_szPhone2;
  CString     m_szEmail;
  CString     m_szNote;

  //Set default values of class members
  void _SetDefaultValues()
    m_szAccountID  = _T("");
    m_szName       = _T("");
    m_szAddress    = _T("");
    m_szPhone1     = _T("");
    m_szPhone2     = _T("");
    m_szEmail      = _T("");
    m_szNote       = _T("");

  //Exchange field values with data provider
  void DoFieldExchange(bool bSave = FALSE)
    FX_VarChar           (bSave, _T("AccountID"),  m_szAccountID);
    FX_VarChar           (bSave, _T("Name"),       m_szName);
    FX_Text              (bSave, _T("Address"),    m_szAddress);
    FX_VarChar           (bSave, _T("Phone1"),     m_szPhone1);
    FX_VarChar           (bSave, _T("Phone2"),     m_szPhone2);
    FX_VarChar           (bSave, _T("Email"),      m_szEmail);
    FX_Text              (bSave, _T("Note"),       m_szNote);


Developing the library has given me better insight into ADO, COM and programming in general. I can say that an honest attempt has been made to produce a workable and bug-free library, but my work/test environment is limited and there is always the one that gets through. Certainly any feedback on errors and usability is welcome. I hope dbAx proves itself useful to all to decide to try it.


  • 21/12/07 v1.0 Initial release
  • 25/01/08 Corrected errors in the AxGen module resulting in bad source code being generated
  • 05/03/08 v1.10 Numerous updates, code fixes, code generation and more; see AxLib.h Revision History for details
  • 01/20/09 v1.2 Bug fixes and code update. See AxLib.h for details


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


About the Author

Ross White
Software Developer
I work almost exclusively with C++ and database applications and manage the IT concerns for the engineering department of a large food processing company. As such, I get involved with add-ons to existing commercial software, SCADA, interfacing with industrial components (PLC’s, automated weight checking machines, etc.) and other pet projects (currently working on v4.0 of my own document management app).

I get totally fed-up, frustrated and thoroughly delighted with programming and computers, but always remember the sage words of an old lab tech from my Air Force days; “There was never a statue erected to the man who left well enough alone.” I’ll be at it again tomorrow!

You may also be interested in...


Comments and Discussions

QuestionHow to support xml Pin
wmywipured28-Oct-13 22:15
memberwmywipured28-Oct-13 22:15 
GeneralMy vote of 5 Pin
pingle30926-Apr-13 23:17
memberpingle30926-Apr-13 23:17 
QuestionBreaking change in MDAC ADODB COM components in Windows 7 Service Pack 1 Problems Pin
maplewang4-Jan-12 16:51
membermaplewang4-Jan-12 16:51[^]

[huseR] suggestions:
The following code snippet for VS C++ uses the deprecated interfaces as default for project, so it should be backwards compatible with ADO interfaces on older versions of Windows.

As far as I am testing my project compiled on Windows7 SP1 x64, it runs and creates ADO interfaces on Windows XP SP3 (x86) and works with the database data, previously it returned hresult E_NOINTERFACE during ADO interface creation (_Connection).

in your "StdAfx.h"

#import <msado15.dll> rename("EOF", "ADO_EOF") \
exclude("_Connection", "_Command", "_Parameter", "_Recordset") \
rename("_Connection_Deprecated", "_Connection") \
rename("_Command_Deprecated", "_Command") \
rename("_Parameter_Deprecated", "_Parameter") \
rename("_Recordset_Deprecated", "_Recordset")
QuestionSeveral Problems Pin
maplewang30-Dec-11 15:03
membermaplewang30-Dec-11 15:03 
AnswerRe: Several Problems Pin
Ross White1-Jan-12 11:36
memberRoss White1-Jan-12 11:36 
GeneralRe: Several Problems Pin
maplewang2-Jan-12 14:50
membermaplewang2-Jan-12 14:50 
GeneralRe: Several Problems Pin
Ross White12-Jan-12 18:43
memberRoss White12-Jan-12 18:43 
QuestiondbAx without SQL server (ie. able to talk to standard Access MDB file?) Pin
Member 839203411-Nov-11 4:39
memberMember 839203411-Nov-11 4:39 
AnswerRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) Pin
Ross White14-Nov-11 6:34
memberRoss White14-Nov-11 6:34 
GeneralRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) Pin
maplewang27-Dec-11 3:31
membermaplewang27-Dec-11 3:31 
GeneralRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) Pin
Ross White1-Jan-12 11:24
memberRoss White1-Jan-12 11:24 
GeneralRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) Pin
maplewang2-Jan-12 14:42
membermaplewang2-Jan-12 14:42 
QuestionAxGen: SQL Server enumeration feature Pin
Grzegorz Kulik3-Oct-11 1:43
memberGrzegorz Kulik3-Oct-11 1:43 
AnswerRe: AxGen: SQL Server enumeration feature Pin
Ross White4-Oct-11 16:57
memberRoss White4-Oct-11 16:57 
QuestionCannot read the CHM file Pin
viaducting27-Jun-11 23:54
memberviaducting27-Jun-11 23:54 
AnswerRe: Cannot read the CHM file Pin
Ross White6-Jul-11 11:37
memberRoss White6-Jul-11 11:37 
GeneralMy vote of 5 Pin
thelvaci9-Dec-10 9:26
memberthelvaci9-Dec-10 9:26 
GeneraldbAx and VC6 Pin
garyt17-Jun-09 14:31
membergaryt17-Jun-09 14:31 
GeneralRe: dbAx and VC6 Pin
Ross White18-Jun-09 6:10
memberRoss White18-Jun-09 6:10 
GeneralBug in CAxRecordset::Open() Pin
Member 27829223-Dec-08 4:42
memberMember 27829223-Dec-08 4:42 
GeneralRe: Bug in CAxRecordset::Open() Pin
Ross White9-Jan-09 6:18
memberRoss White9-Jan-09 6:18 
Questiongreate lib but rely on CString and oledatetime? Pin
maplewang26-Aug-08 15:41
membermaplewang26-Aug-08 15:41 
AnswerRe: greate lib but rely on CString and oledatetime? Pin
Ross White27-Aug-08 10:30
memberRoss White27-Aug-08 10:30 
AnswerRe: greate lib but rely on CString and oledatetime? Pin
Rolf Kristensen25-Jun-10 14:15
memberRolf Kristensen25-Jun-10 14:15 
GeneralFYI - Clean Compile and Link using VS2K8 32 & 64 bit XP & Vista Pin
1000mp17-May-08 4:46
member1000mp17-May-08 4: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
Web02 | 2.8.180111.1 | Last Updated 21 Jan 2009
Article Copyright 2007 by Ross White
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid