Click here to Skip to main content
Click here to Skip to main content

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

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

Introduction

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.

Background

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.

AxGen

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 (www.dmsic.com)

    DESCRIPTION
    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.

    DISCLAIMER
    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
{
public:
  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);
  };
};

Conclusion

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.

History

  • 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

License

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

Share

About the Author

Ross White
Software Developer
Unknown
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!

Comments and Discussions

 
QuestionHow to support xml Pinmemberwmywipured28-Oct-13 22:15 
GeneralMy vote of 5 Pinmemberpingle30926-Apr-13 23:17 
QuestionBreaking change in MDAC ADODB COM components in Windows 7 Service Pack 1 Problems Pinmembermaplewang4-Jan-12 16:51 
QuestionSeveral Problems Pinmembermaplewang30-Dec-11 15:03 
AnswerRe: Several Problems PinmemberRoss White1-Jan-12 11:36 
GeneralRe: Several Problems [modified] Pinmembermaplewang2-Jan-12 14:50 
GeneralRe: Several Problems PinmemberRoss White12-Jan-12 18:43 
QuestiondbAx without SQL server (ie. able to talk to standard Access MDB file?) PinmemberMember 839203411-Nov-11 4:39 
AnswerRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) PinmemberRoss White14-Nov-11 6:34 
GeneralRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) [modified] Pinmembermaplewang27-Dec-11 3:31 
GeneralRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) PinmemberRoss White1-Jan-12 11:24 
GeneralRe: dbAx without SQL server (ie. able to talk to standard Access MDB file?) Pinmembermaplewang2-Jan-12 14:42 
QuestionAxGen: SQL Server enumeration feature PinmemberGrzegorz Kulik3-Oct-11 1:43 
AnswerRe: AxGen: SQL Server enumeration feature PinmemberRoss White4-Oct-11 16:57 
QuestionCannot read the CHM file Pinmemberviaducting27-Jun-11 23:54 
AnswerRe: Cannot read the CHM file PinmemberRoss White6-Jul-11 11:37 
GeneralMy vote of 5 Pinmemberthelvaci9-Dec-10 9:26 
GeneraldbAx and VC6 Pinmembergaryt17-Jun-09 14:31 
GeneralRe: dbAx and VC6 PinmemberRoss White18-Jun-09 6:10 
GeneralBug in CAxRecordset::Open() PinmemberMember 27829223-Dec-08 4:42 
GeneralRe: Bug in CAxRecordset::Open() PinmemberRoss White9-Jan-09 6:18 
Questiongreate lib but rely on CString and oledatetime? Pinmembermaplewang26-Aug-08 15:41 
AnswerRe: greate lib but rely on CString and oledatetime? PinmemberRoss White27-Aug-08 10:30 
maplewang wrote:
rely on CString and oledatetime

They were too handy to pass up. It was assumed that most development work would be using VS Professional or better (and probably MFC).
AnswerRe: greate lib but rely on CString and oledatetime? PinmemberRolf Kristensen25-Jun-10 14:15 
GeneralFYI - Clean Compile and Link using VS2K8 32 & 64 bit XP & Vista Pinmember1000mp17-May-08 4:46 
GeneralRe: FYI - Clean Compile and Link using VS2K8 32 & 64 bit XP & Vista PinmemberRoss White20-May-08 5:13 
QuestionWhat suitable databound GUI controls do you recommend for Win32 C++ OLE DB/ADO database applications? PinmemberAlexandru Matei18-Mar-08 5:08 
AnswerRe: What suitable databound GUI controls do you recommend for Win32 C++ OLE DB/ADO database applications? PinmemberRoss White18-Mar-08 9:02 
QuestionDatabase related files are missing. PinmemberGarudappa6-Mar-08 17:59 
GeneralRe: Database related files are missing. PinmemberRoss White7-Mar-08 5:38 
GeneralError in FX_Integer(bool bSave, LPCTSTR lpszColumn, int& value) PinmemberRoMuc2-Feb-08 19:32 
GeneralRe: Error in FX_Integer(bool bSave, LPCTSTR lpszColumn, int& value) PinmemberRoss White3-Feb-08 17:30 
QuestionImage other than BMP PinmemberPatrickquinn121230-Jan-08 5:24 
GeneralRe: Image other than BMP PinmemberRoss White30-Jan-08 6:47 
AnswerRe: Image other than BMP PinmemberJim Crafton21-Jan-09 11:59 
QuestionWhy the CAxRecordset::Open function does'nt call my own DoFieldExchange(FALSE) function? PinmemberJason.Han24-Jan-08 2:35 
AnswerRe: Why the CAxRecordset::Open function does'nt call my own DoFieldExchange(FALSE) function? PinmemberJason.Han24-Jan-08 16:18 
AnswerRe: Why the CAxRecordset::Open function does'nt call my own DoFieldExchange(FALSE) function? PinmemberRoss White24-Jan-08 18:27 
GeneralAXGen PinmemberRoMuc30-Dec-07 2:21 
GeneralRe: AXGen PinmemberRoss White30-Dec-07 15:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 21 Jan 2009
Article Copyright 2007 by Ross White
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid