Extended OLEDB library : Ease of ADO, power of OLEDB

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

.

Environment:  : VC++6 SP5, MDAC 2.5 and above

Overview


Most of us know that OLEDB is the latest database technology that provides
fastest data access on windows platform. However, many C++ programmers
tend to avoid using raw OLEDB and prefer ADO in their development process. There are two main reasons for this. First of all, OLEDB interfaces (which are COM interfaces) are extremely complex and sparsely documented. They are neither convenient nor
developer-friendly. So, we are left with other choice : use ATL consumer
templates for OLEDB. These template classes are very powerful and high
performance. However, they lack the flexibility and rich functionality
provided by ADO. Many times one has to resort to ADO for some user interface
operations such as binding a rowset to datagrid or other controls. This is
because there is no direct way to bind an OLEDB rowset to a control.

This article presents a set of classes that are extended from ATL
consumer template classes for OLEDB. While these classes provide added
flexibility and functionality like ADO, they still retain the power and
performance of consumer template classes. Here, in this article, I present
only a subset of a complete libray that we have built for commercial
product development.

[1] Connection object : CSypODLConnection

In OLEDB consumer templates, you have to deal with two classes for
maintaining database connection and transaction purposes : CDataSource and
CSession. This is sometimes confusing. Instead, we created a single class
called CSypODLConnection that is very much like Connection object in ADO.
This class combines the functionality of database connection and
transaction maintenance. The use of this class is pretty simple :

// Define connection object
CSypODLConnection m_Cnn;

// Connnect to database
m_Cnn.Open(“CONNECTION STRING”, “UID”, “PWD”);

You can also use transactions on this object like ADO connection object

m_Cnn.StartTransaction(); // Connnect to database
— Code —

m_Cnn.Commit();

Also, like ADO connection object, we have provided Execute method for executing action queries.

m_Cnn.Execute(“DELETE FROM Customer WHERE CustomerID = 5”);

[1] RecordBase, Exception, and Error classes


Similarly, we have also created a recordset class called
CSypODLRecordBase, which wraps up CCommand and CTable comsumer template
classes. Those who have worked with these consumer template classes
know how tedious it is to check the HRESULT value after each method call.
Not only is it time-consuming to check HRESULT value at every point, but also it
is extremely difficult to get errors from IErrorInfo interface. In order to
solve this problem, we created two classes : an exception class and an
error-processing class. So, the code becomes very simple as shown below :

CSypODLRecordBase<CAccessor<CSalesManTableAccessor> > m_Rs;
// Process data now
try
{
m_Rs.Open(“SalesMan”, &m_Cnn, 1); // 1 indicates table
while (!m_Rs.IsEOF())
{
AfxMessageBox(m_Rs.GetAccessor()->m_SalesManName);
m_Rs.MoveNext()
}
m_Rs.Close();
}

catch(CSypODLException e)
{
e.DisplayError();
return;
}
//

Essentially, if you already know ADO, this library will
be easiest to understand and use. So, if you want to give your application a
lightening speed and high performance, use these classes.

Actually, there are so many features and so many classes
in the library that we can write a complete a book on it. However, due to
lack of space and scope, I urge users to go through the code which is
self-explanatory. I would also advise you to study the included sample, and see
how easy it is to use these classes. If you need a complete documentation, you
may contact me directly.

Known drawbacks and limitations

There seems to be bugs in some OLEDB providers that may affect some of the functionality
of this library. For some reason, the Update method and bookmarks
fail with certain providers.

If you decide to use this code

You are free to use or modify this code subject to the following
restrictions:

* You must acknowledge us somewhere in your about box or documentation,
simple “Parts of code by..Sypram Technology” will be enough. If you cannot (or
don’t want to) mention us, contact one of us personally.

* Do not remove copyright notices from the source and header files.

* Do not publish any part of this code or article on other websites.

* We reserve exclusive right to update this page, as well as provided source
code. Please publish your modification and additions on adjacent pages. We may
add your modifications and/or additions to a future update to the article and
source with proper credit for your work.

Downloads


Download demo project – 57 Kb


Download source – 16 Kb

More by Author

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Must Read