Using ADO from C++
Using ADO from C++ Environment: Visual C++ 6
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++ ProgramOne 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:
#
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:
#
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.
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.
About the Author
computer science & engineering, CUET
Go to page: 1 2 Next
Downloads
Database.zip - example source code for this article