CMyADO'�Making Life Easy for the ADO Coder

Environment: Visual C++ 6.0


Welcome to the MyADO Wrapper article written for CodeGuru. The article contents and the knowledge gained were mostly due to reading other articles on this site, including Bob Place's ADO is AOK article and a few other ADO/Misc DB contributions. I will not explain everything that has already been covered in these articles, but I will demonstrate a class I created for use with ADO and stored procedures which I find very handy. I will also not explain how to set up an SQL database to use stored procedures, as that has also been covered elsewhere on CodeGuru. I will, however, provide all resources required for this article.

Database Setup

This article uses a DSN and table named TestMyADO, and the following can be used to create the following table:

Create Table TestMyADO
  ID Int Primary Key Identity,
  Name VarChar(15) Not Null,
  Value VarChar(15) Not Null

with the following stored procedures:

Create Procedure InsertMyADO
  @ID Int Out,
  @Name VarChar(15),
  @Value VarChar(15)
  Insert Into TestMyADO (Name, Value) Values (@Name, @Value)
  Select @ID = @@Identity
  Return @@Identity

Create Procedure SelectMyADO
  Select ID, Name, Value From TestMyADO

The InsertMyADO stored procedure is used to insert a record into TestMyADO and uses a return value, along with an output variable (ID) and two input variables (Name/Value). The SelectMyADO stored procedure creates a recordset that selects all records from TestMyADO.


CMyADO is a class created to make life easy when using ADO. The basic usage is that you open a connection, initialize a stored procedure, add parameters, execute the stored procedure, and get the results. Integrating this class into your code is simple. Merely #include "MyADO.h", initialize COM, and instantiate an object.

// Instantiate the Object

Next, open the connection and initialize the stored procedure. Always be sure to validate against the return value.

// Open( ConnectionString, UserID, Password ) the Connection
if( MyADOObject.Open( "TestMyADO", "", "" ) == S_OK )
  // Initialize( StoredProcedureName ) the Stored Procedure
  if( MyADOObject.Initialize( "InsertMyADO" ) == S_OK )

After the stored procedure is initialized, you can add parameters.

    // Add the Return Value Parameter
    if( MyADOObject.AddParameterReturnValue() == S_OK )
      // Add the Output Long Parameter
      if( MyADOObject.AddParameterOutputLong( "ID" ) == S_OK )
        // Add the Input Text Parameters
        if( MyADOObject.AddParameterInputText(
            "Name", "Test Name" ) == S_OK &&
            "Value", "Test Value" ) == S_OK )

Next, you'll want to execute the stored procedure and retrieve results (Return Value, Output Paramters, Recordset Fields).

          // Execute the Stored Procedure
          if( MyADOObject.Execute() == S_OK )
            long lReturnValue = 0;
            long lID = 0;

            // Retrieve the Return Value and the Output Paramter
            // set up above
            if( MyADOObject.GetParameterReturnValue( &lReturnValue )
               == S_OK &&
                MyADOObject.GetParameterLong( "ID", &lID ) == S_OK )
              // Sanity check that does nothing : )
              if( lReturnValue == lID )
                printf( "Inserted Record with ID: %2d, Name: %15s,
                         Value: %15s\n", lID, pNames[dwIndex],
                         pValues[dwIndex] );

And there you have it; easy as pie. Using the SelectMyADO stored procedure is even easier; there are no parameters. It does return a recordset, though, and the fields can be retrieved in a similar and consistent manner.

First, we re-initialize and execute the same ADO object.

// Initialize( StoredProcedureName ) the Stored Procedure
if( MyADOObject.Initialize( "SelectMyADO" ) == S_OK )
  // Execute the Stored Procedure
  if( MyADOObject.Execute() == S_OK )

Then, we loop through each record and retrieve the recordset fields.

    // Ensure there are more Records to Retrieve
    while( !MyADOObject.IsEOF())
      long lID = 0;
      char szName[15];
      char szValue[15];

      // Retrieve the Record Fields
      if( MyADOObject.GetFieldLong( "ID", &lID ) == S_OK &&
          MyADOObject.GetFieldText( "Name", szName, 
                                     sizeof( szName )) == S_OK &&
          MyADOObject.GetFieldText( "Value", szValue,
                                     sizeof( szValue )) == S_OK )
        printf( "Selected Record with ID: %2d, Name: %15s, Value:
                 %15s\n", lID, szName, szValue );

      // Move to the Next Record

I hope you have found this article and source useful and you take the time to delve deeper into the class to understand how it works. I leave up to you the task of adding functionality to this class for other variable types, as you need them.


If you use this class and get an IDispatch #3092 error, there's a good chance you either added parameters incorrectly (for example, return values need to be added first in many cases), or you did not create the table or stored procedures in the correct database.


Download demo project - 32 Kb


  • Will not compile on VC 7.0 or 8.0. What am I missing?

    Posted by Mike Pliam on 04/03/2008 02:33pm

    This code will not compile on VC 7.0 or VC 8.0.  I did change the import library to #import "c:\Program Files\Common Files\System\ADO\msado15.dll" rename("EOF", "EndOfFile"), and that is working because it works in other applications and the type libraries show up in the Release and Debug directories.
    Get 69 errors, mostly related to undeclared identifers, i.e., _ParameterPtr,  adCmdStoredProc, adInteger, adParamInput, etc, ... and syntax errors related to DataTypeEnum.
    Perhaps I am missing some typedefs or a header file?
    Can't believe there are not more comments on this code.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date