Home arrow C++ arrow Page 3 - ActiveX Data Objects (ADO) In Visual C++

ActiveX Data Objects (ADO) In Visual C++

If you've ever wondered how to connect to a database using ADO in Microsoft Visual C++ then look no further. In this excellent step-by-step guide, Neville shows us how to do just that...

Author Info:
By: Neville Mehta
Rating: 5 stars5 stars5 stars5 stars5 stars / 46
October 27, 2002
  1. · ActiveX Data Objects (ADO) In Visual C++
  2. · The Myriad of Data Access Technologies
  3. · An ADO Application in VC
  4. · Writing the Function
  5. · Conclusion

print this article

ActiveX Data Objects (ADO) In Visual C++ - An ADO Application in VC
(Page 3 of 5 )

Create a new ATL COM AppWizard project. Make a Dynamic Link Library, not an Executable. Once the project is created, choose New ATL object from the Insert menu. Choose Simple Object from the list. You an provide any name for this object that you desire.

Select the interface name in the Class View. Right click and choose Add Method. Enter the method name as getRecordSet. In the parameters text field enter the following string:

[in]BSTR tablename,[out,retval] _Recordset **ptr

This denotes that the first parameter to be passed is the table name. It is of BSTR type (which is a type of String). The second one is an output parameter. It denotes that this method will return a Smart Pointer of _Recordset type.

Our next step is to import the ADO library into our project. After all, we are going to use ADO to access our data source (which is in our case an MS Access database). We enter the following lines of code in the StdAfx.h file:

#import "c:\program files\common files\system\ado\msado15.dll" rename_namespace("USEADO") rename("EOF","EndOfFile")
using namespace USEADO;

In the first line we use the import keyword to import msado15.dll. This is the ADO DLL, which we have to import if we wish to use any of the ADO methods. Whenever we wish to use a DLL in our project -- apart from importing it -- we need to specify its namespace name. Since we didn't create the msado15.dll file, we wouldn't know its namespace name, thus we use the rename_namespace function to rename the ADO DLL's namespace to "USEADO".

Again, there are a few syntax's or keywords that Visual C++ doesn’t recognize that are a part of the DLL. One of them is the EOF keyword, which denotes to the program that the database has no more records. Visual C++ doesn't recognize EOF, thus we rename it to something that Visual C++ recognizes. We use the rename function to rename "EOF" to "EndOfFile", which is the syntax that Visual C++ recognizes. In the second line we specify the project that we are using the USEADO namespace.

After all the editing, our StdAfx.h file would look like this:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently

#if !defined(AFX_STDAFX_H__4F895F84_E87C_11D6_BCCE_0040F42DD16F__INCLUDED_)
#define AFX_STDAFX_H__4F895F84_E87C_11D6_BCCE_0040F42DD16F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define STRICT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400

#include <afxwin.h>
#include <afxdisp.h>

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

#import "c:\program files\common files\system\ado\msado15.dll" rename_namespace("USEADO") rename("EOF","EndOfFile")
using namespace USEADO;

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__4F895F84_E87C_11D6_BCCE_0040F42DD16F__INCLUDED)

Next we have to make a major change in the IDL file. You will find an IDL file in the list of source files of your project. Double click on this file and you will see the entire IDL code. The IDL file has the declaration of your method. The problem is that this method returns a smart pointer of _Recordset type. The IDL file needs to understand what a _Recordset object is, and thus we need to import the msado15.dll file into here too.

Inside the library declaration we need to add this line:

importlib("c:\program files\common files\system\ado\msado15.dll");

We use the import lib statement to include the msado15.dll in our IDL file. We also cut and paste the interface definition from outside the library block into the library block.

After the copy paste operation the IDL would look like this:

// AdoAccess.idl : IDL source for AdoAccess.dll

// This file will be processed by the MIDL tool to
// produce the type library (AdoAccess.tlb) and marshalling code.

import "oaidl.idl";
import "ocidl.idl";
helpstring("AdoAccess 1.0 Type Library")

library ADOACCESSLib
importlib("c:\program files\common files\system\ado\msado15.dll");

helpstring("IadoNow Interface"),
interface IadoNow : IDispatch
[id(1), helpstring("method getRecordSet")] HRESULT getRecordSet([in]BSTR tablename,[out,retval] _RecordSet **ptr);

helpstring("adoNow Class")
coclass adoNow
[default] interface IadoNow;

After editing the IDL file, we need to move on to writing our actual function, which is what we will do next.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials