Creating an OLE DB Consumer

You can use the ATL COM AppWizard and ATL Object Wizard to generate an OLE DB Template consumer.

To create an ATL project for an OLE DB consumer

  1. From the File menu, choose New. The New dialog box appears.

  2. On the Projects tab, choose ATL COM AppWizard.

    Name the project on the Projects tab, then click OK. The ATL COM AppWizard appears.

  3. In the ATL COM AppWizard, choose a project type.

  4. Click Finish and OK.

Next, use the ATL Object Wizard.

To use the ATL Object Wizard

  1. From the Insert menu, choose New ATL Object. The ATL Object Wizard appears.

  2. In the left pane, choose Data Access. The Consumer icon appears in the right pane.

  3. Select the Consumer icon and click Next. The ATL Object Wizard Properties dialog box appears.

Use the ATL Object Wizard to set properties for your consumer.

To set consumer properties using the ATL Object Wizard

  1. In the ATL Object Wizard Properties dialog box, click the Select Datasource button. The Create New Datalink dialog box appears.

  2. In the Create New Datalink dialog box, select an OLE DB Provider and appropriate options, including:
    • Location and data source for the data to be used by the Provider.

    • Logon ID and password for your data source.

    After selecting your provider and other settings, click Test Connection to verify the selections made on the previous dialog box pages. If the Results box reports PASSED, click Finish to create the data link. The Select Database Table dialog box appears.

  3. In the Select Database Table dialog box, use the tree control to select a table, query, or stored procedure and click OK. You return to the ATL Object Wizard Properties dialog box.

  4. The dialog box fills in the names for the Short Name, Class, Accessor, and .H File. The last four are based on the short name. You can edit these names if desired.

  5. The wizard creates a CCommand-based consumer by default. If you prefer a CTable-based consumer, select the Table button.

  6. Choose Change, Insert, and Delete to support the changing, inserting, and deleting of records in the rowset, if required.

  7. Click OK.

The wizard generators an accessor class and a command (or table) class, as shown in the sample output below. The command class contains code to open the data source and rowset you specified in the wizard. The accessor class contains a column map for the database table you selected.

// Product.H : Declaration of the CProduct class
#ifndef __PRODUCT_H_
#define __PRODUCT_H_
class CProductAccessor
{
public:
   LONG m_ProductID;   // Number automatically assigned to new product.
   TCHAR m_ProductName[41];   // Number automatically assigned to new product.
   LONG m_SupplierID;   // Same entry as in Suppliers table.
   LONG m_CategoryID;   // Same entry as in Categories table.
   TCHAR m_QuantityPerUnit[21];   // (e.g., 24-count case, 1-liter bottle).
   DB_NUMERIC m_UnitPrice;   // (e.g., 24-count case, 1-liter bottle).
   SHORT m_UnitsInStock;   // (e.g., 24-count case, 1-liter bottle).
   SHORT m_UnitsOnOrder;   // (e.g., 24-count case, 1-liter bottle).
   SHORT m_ReorderLevel;   // Minimum units to maintain in stock.
   VARIANT_BOOL m_Discontinued;   // Yes means item is no longer available.

BEGIN_COLUMN_MAP(CProductAccessor)
   COLUMN_ENTRY(1, m_ProductID)
   COLUMN_ENTRY(2, m_ProductName)
   COLUMN_ENTRY(3, m_SupplierID)
   COLUMN_ENTRY(4, m_CategoryID)
   COLUMN_ENTRY(5, m_QuantityPerUnit)
   COLUMN_ENTRY(6, m_UnitPrice)
   COLUMN_ENTRY(7, m_UnitsInStock)
   COLUMN_ENTRY(8, m_UnitsOnOrder)
   COLUMN_ENTRY(9, m_ReorderLevel)
   COLUMN_ENTRY_TYPE(10, DBTYPE_BOOL, m_Discontinued)
END_COLUMN_MAP()
DEFINE_COMMAND(CProductAccessor, _T(" \
   SELECT \
      ProductID, \
      ProductName, \
      SupplierID, \
      CategoryID, \
      QuantityPerUnit, \
      UnitPrice, \
      UnitsInStock, \
      UnitsOnOrder, \
      ReorderLevel, \
      Discontinued  \
      FROM Products"))};
class CProduct : public CCommand<CAccessor<CProductAccessor> >
{
public:
   HRESULT Open()
   {
      HRESULT      hr;
      hr = OpenDataSource();
      if (FAILED(hr))
         return hr;
      return OpenRowset();
   }
   HRESULT OpenDataSource()
   {
      HRESULT      hr;
      CDataSource db;
      CDBPropSet   dbinit(DBPROPSET_DBINIT);
      dbinit.AddProperty(DBPROP_INIT_DATASOURCE, OLESTR("OLE_DB_NWind_Jet"));
      dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
      dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
      hr = db.Open(_T("MSDASQL"), &dbinit);
      if (FAILED(hr))
         return hr;
      return m_session.Open(db);
   }
   HRESULT OpenRowset()
   {
      // Set properties for open
      CDBPropSet   propset(DBPROPSET_ROWSET);
      propset.AddProperty(DBPROP_IRowsetChange, true);
      propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE);
      return CCommand<CAccessor<CProductAccessor> >::Open(m_session, NULL, &propset);
   }
   CSession   m_session;
};
#endif // __PRODUCT_H_

Back to Using the OLE DB Consumer Templates