OLE DB Templates - A light and simple Data Access Method

I was amazed and delighted to see how simple database access has become thanks to OLE DB. I've been reading the hype and so I went out and found the Microsoft Book on OLE DB. It's a great idea as it's explained there but still rather complex to code. I noticed that Visual C++ 6 has some new templates for Data Consumers so I started looking at the documentation. Wow! Is this simple stuff or what??

Here's a sample, a CONSOLE application at that, using the NWind database that comes with the DASDK. It's only 99 lines of code including comments and it compiles into a 60K executable.


 
//file: olsamp.cpp 
//auth: dick warg 
//date: 6/25/99 
//func: minimal oledb program

#include <atldbcli.h>
#include <iostream>
using namespace std;

// define a class to hold the data from the table
class myNwCust
{
public:
    // data elements
    TCHAR m_CustomerID[6];
    TCHAR m_CompanyName[41];
    TCHAR m_ContactName[31];
    TCHAR m_Phone[25];

    // column binding  -- I only want these 4 fields
    BEGIN_COLUMN_MAP(myNwCust)
        COLUMN_ENTRY(1, m_CustomerID)
        COLUMN_ENTRY(2, m_CompanyName)
        COLUMN_ENTRY(3, m_ContactName)
        COLUMN_ENTRY(4, m_Phone)
    END_COLUMN_MAP()
};

// declare the OLEDB objects
CDataSource ds;
CSession    session;
CCommand <CAccessor<myNwCust> > cust;

int main()
{

try{
    // fire up COM
    HRESULT hr = CoInitialize(0);
    if(FAILED(hr))
    {
        cout << "Can't start COM!? " << endl;
        return -1;
    }
    
    // connect to the database
    hr = ds.Open(_T("MSDASQL"), "OLE_DB_NWind_Jet", "sa", "");
    if(FAILED(hr))
    {
        cout << "Can't open Nwind" << endl;
        return -1;
    }

    // start the session
    hr = session.Open(ds);
    if(FAILED(hr))
    {
        cout << "Can't open Nwind SESSION" << endl;
        ds.Close();
        return -1;
    }

    // construct the query string
    TCHAR mySQL[] = "SELECT CustomerID, CompanyName, ContactName, \
    Phone FROM Customers";

    // open the dataset
    hr = cust.Open(session, mySQL);
    if(FAILED(hr))
    {
        cout << "Can't open Nwind TABLE" << endl;
        session.Close();
        ds.Close();
        return -1;
    }

    // read all the data
    while(cust.MoveNext() == S_OK)
    {
        cout << cust.m_CustomerID << ", " << cust.m_CompanyName << ", "; 

        cout << cust.m_ContactName << ", " << cust.m_Phone << endl;
    }

    cust.Close();
    session.Close();
    ds.Close();

    cout << "That's All Folks" << endl;

    return 1;
}
catch(...)
{
    cout << "Unknown failure" << endl;
    return -1;
}
}

The Microsoft documentation and samples are in the MSDN pages "Visual C++ Documentation \ References \ Microsoft Foundation Class Library and Templates\OLE DB Templates"


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • IT decision support impacts all aspects of technology management, from governance and strategy to budgets and resource planning. IT decision support effectiveness often falls prey to data-driven challenges that make it difficult to understand the data in context. These challenges: overwhelming data volumes, heterogeneous data types, and growing data complexity. This Forrester Consulting Paper reports the three key findings from their study conducted, on behalf of BDNA, to test the hypothesis that data …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds