Microsoft ActiveX Data Object (ADO) provides an easy way to data access and manipulation that is independent of data stores, tools, and languages. This flexibility and easy-to-code facility makes ADO the perfect choice for developers. ADO is implemented with Component Object Model (COM) interfaces. Unlike VB programmers, C++ programmers must know the details of using COM for using ADO. So, using ADO from C++ is still very complex. But, it is possible to get an easy ADO programming model from C++, which can help to hide the details of using COM. In this article, I demonstrate a C++ class to do this that encapsulates the ADO connection object. You can apply same technique for encapsulating other ADO objects.
Adding ADO Support to a C++ Program
One way to add ADO support to your C++ program is to import ADO type library information. The type library for the current ADO is contained within the ADO DLL, msado15.dll. You can import this type library by importing msado15.dll by using the #import directive:
#import "c:Program FilesCommon FilesSystemADOmsado15.dll" rename("EOF", "EndOfFile")
Of course, you may need to change the path of msado15.dll according to the location where the file is found on your machine. Because I don’t mention the no_namespace option, the directive generates a type library header (generally msado15.tlh) that contains typedef declarations, smart pointers for interfaces, and enumerators under the ADODB namespace. The rename macro tells the preprocessor to replace the EOF constants by EndOfFile. ADO defines nine objects (Connection, Command, Recordset, Record, Stream, Parameter, Field, Property, and Error) and four collections (Parameters, Fields, Properties, and Errors). The #import directive generates smart pointer-type declarations for all the ADO objects. For example, a variable that points to a _Connection object is of the _ConnectionPtr type. So, if you want to refer to the _Connection object, you have to declare a variable of the _ConnectionPtr type.
ADODB::_ConnectionPtr Cnn;
Now, create a C/C++ header file named .Database.h. and add the codes given below:
#import "c:Program FilesCommon FilesSystemADOmsado15.dll" rename("EOF", "EndOfFile") typedef ADODB::_RecordsetPtr RecPtr; typedef ADODB::_ConnectionPtr CnnPtr;
Notice that all typedef declarations, smart pointers for interfaces, and enumerators are in msado15.tlh. The compiler automatically generates this file after compiling the above #import directive. You don’t need to include this file because C++ is responsible for this file. Now, we are ready to declare a class, which encapsulates the ADO Connection object. Let’s write the code for the class that encapsulates the ADO Connection object.
class Database
{
public:
CnnPtr m_Cnn;
Database();
~ Database();
bool Open(char* UserName, char* Pwd, char* CnnStr, char* ErrStr);
RecPtr Execute(char* CmdStr);
bool Close();
};
Actually, the ADO Connection object has many methods. But, to simplify the code, I declare only three methods (Open, Close, and Execute) to give access to three methods of the connection object: Open establishes a physical connection to a data source, Execute executes a command or stored procedure on the established connection, and Close breaks the established connection.