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
Name the project on the Projects tab, then click OK. The ATL COM AppWizard appears.
Next, use the ATL Object Wizard.
To use the ATL Object Wizard
Use the ATL Object Wizard to set properties for your consumer.
To set consumer properties using the ATL Object Wizard
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.
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_