<!-- Main HTML starts here -->
Recently I did a project
on online banking at NIIT, Bangalore. The project was coded mostly using VB with a few ATL
components thrown in, if only to teach us programming distributed applications. One middle tier component that
I programmed was built with ATL and uses ADO to query the backend (SQL Server). Parts of that
code appears here.
I assume the reader knows or at least has a fair idea about COM
programming using ATL as well as ADO programming with VB.
What is ADO?
ADO stands for ActiveX Data Objects. ADO provides an object-oriented
programming interface for accessing a data source using the OLEDB data provider.
It is the succesor to DAO and RDO object models and combines the best features
DAO and RDO.
Programming OLEDB in C++ is easy. However, for languages like Visual Basic, that do not support
pointers and other C++ features, implementing OLEDB is difficult.
This is where ADO really shines. ADO is an high level interface to OLEDB that is based on COM
interfaces. Thus any application that supports COM can implement ADO.
- Access to all types of data - Various data sources include e-mail, text files, RDBMSs, ISAM/VSAM databases and all ODBC data sources.
- Supports free threading - ADO supports multiple client connections through multiple threads in such a way that these threads do not interfere with each other.
- Supports asynchronous queries - This basically means that after an SQL query is submitted to the database server, the control returns immediately to the calling application, allowing the user to continue working while the query is being processed. On completion of the query, the results are sent to the client.
- Supports client-side and server-side cursors - Cursor is a mechanism that allows access and navigation of data in a recordset. They are implemented as client-side or server-side. Traditionally, frequently updated recordsets are implemented as server-side while read-only recordsets are implemented as client-side.
- Supports disconnected recordsets - After a recordset is returned on execution of a query, it is stored as a client-side cursor and the active connection is closed. After changes have been commited to the recordset, the connection is reestablished and all updates are sent in a batch to the data store. This helps in reducing network traffic to a great extent.
- Supports Commands as connection methods - An unique feature of ADO is that when a command is executed, a connection is first established internally before that command gets submitted for execution. Compare this to traditional object models like DAO/RDO where a connection has to be established explicitly before a command can be submitted.
In the ADO model, we'll be using three main types of objects-
The Connection object sets up a connection to the data source. First, the data source name, its location, user
ID, password etc is stored in a ConnectionString object, which is passed
to the Connection object.to establish a connection to the
The Command object is used to execute SQL commands, queries and stored
When a query is executed, it returns results that are
stored in the Recordset object. Data in a recordset can be manipulated and then
updated to the database.
First, we'll be building an ATL DLL component. This component has a
method that takes one input parameter (customer ID in the project) and returns a reference
corresponding Recordset object to the VB client. The client then
displays the data in a form.
To create the DLL, use the ATL COM AppWizard to generate
the framework for the application. Name the project FindCust
and choose the server type as Dynamic Link
Library. Also choose the option to
support MFC library.
Insert a New ATL Object of type
Simple Object to the project. Use the name Search
in the Short Name textbox of the ATL Object
Wizard Properties and click OK to add the object.
In classview, right click the interface name and add a
method. Name the method
SearchCust and type the following in
[in] BSTR bstrcustid,[out,retval] _Recordset **ptr
Click the OK button to add the method.
SearchCust method returns a reference to a
Recordset object, we need to import the ADO library. To do this, open the file,
StdAfx.h and add the following code :