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: %15sn”, 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:
%15sn”, 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

More by Author

Must Read