Microsoft KB Archive/247003

From BetaArchive Wiki
Knowledge Base

ATLShape.exe: Retrieve Hierarchical Data in an ATL OLE DB Consumer Through the MSDataShape Provider

Article ID: 247003

Article Last Modified on 8/5/2004


  • Microsoft Data Access Components 2.1
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.6
  • Microsoft Data Access Components 2.7
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition

This article was previously published under Q247003


The ATLShape.exe sample demonstrates how to retrieve hierarchical data by using the MSDataShape provider. The sample reads data from either a Microsoft Access 97 database or a SQL Server 7.0 database, and writes it to an Example.out text file.

Active Template Library (ATL) OLE DB Consumer Libraries do not provide support for hierarchical queries. The ATLShape.exe sample provides an ATL OLE DB CMyRowset class that is derived from CRowset, and that incorporates support for hierarchical queries.


The following file is available for download from the Microsoft Download Center:

Release Date: 1-14-2000

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

The ATLShape.exe file contains the following files:

File Name Size
ATLShape.cpp 15KB
Test.mdb 454KB
Readme.txt 15KB

The ATLShape.exe sample allows the user to read from either the Access 97 Test.mdb database or from a local SQL Server 7.0 Northwind database. The hierarchical output is written to an Example.out text file. This is achieved by setting up an appropriate connection string for each:

For the Access 97 Database:

connection.OpenFromInitializationString(L"Provider=MSDataShape.1;Persist Security Info=False;
Data Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\Test.mdb")

For the SQL Server 7.0 Database:

connection.OpenFromInitializationString(L"Provider=MSDataShape.1;Persist Security Info=False;
Data Provider=SQLOLEDB;Database=Northwind;Server=MySQLServer7;uid=sa;pwd=")

ATLShape also creates a CMyRowset class that is derived from the CRowset class. The CRowset class is provided by ATL OLE DB template libraries, which provide the ability to perform hierarchical queries. This class maintains an m_hChapter HCHAPTER variable that stores the HCHAPTER data type of the chaptered column in the parent that is currently being accessed.

The child rowset is obtained by performing an IRowsetInfo::GetReferencedRowset method on the chaptered column as follows:

CComPtr<IRowset> spChildRowset;   
CComPtr<IUnknown> spUnkRowset;
CComPtr<IRowsetInfo> spParent;
spParent->GetReferencedRowset(colNo, __uuidof(IRowset), &spUnkRowset);

In addition, an ATLOutputData template function is provided that reads data from class rowsetClass and writes it to the FILE* that is passed to it:

template <class rowsetClass, class dataSource> 
HRESULT ATLOutputData(rowsetClass &Rs, dataSource &db , FILE *fptr); 

To retrieve data from the chaptered column, perform the following steps:

  1. Instantiate the CMyRowset class:

    CAccessorRowset<CDynamicAccessor, CMyRowset> tmpRowset;
  2. Assign the m_spRowset variable to contain the child rowset:

    tmpRowset.m_spRowset = pChildIRowset;
  3. Assign the m_hChapter variable to the appropriate chaptered column:

    tmpRowset.m_hChapter = *(HCHAPTER*) LocalCommand.GetValue(_bstr_t( L"EmpOrders"));
  4. Bind the output columns:

    hr = tmpRowset.Bind();
  5. Retrieve the data for the first row of the chaptered column:

    hr = tmpRowset.MoveFirst();
  6. Retrieve all the data from the child and write it to the file:

    hr = ATLOutputData(tmpRowset,*dummy,fptr);

Steps to Run the Application

  1. Create an empty Win32 console application.
  2. Insert the ATLShape.cpp file into the project.
  3. Copy the Test.mdb file into the project folder.
  4. Compile and run the application.

Additional query words: ATLShape

Keywords: kbinfo kbconsumer kbdatabase kbfile kbsample kbdtl kbfaq KB247003