Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q247003

↑ Back to the top


Summary

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.

↑ Back to the top


More information

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:

Collapse this tableExpand this table
File NameSize
ATLShape.cpp15KB
Test.mdb454KB
Readme.txt15KB


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;
cLocalCommand.m_spRowset->QueryInterface(&spParent);
spParent->GetReferencedRowset(colNo, __uuidof(IRowset), &spUnkRowset);
spUnkRowset->QueryInterface(&spChildRowset);
				
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.

↑ Back to the top


Keywords: kbdatabase, kbdtl, kbfaq, kbfile, kbinfo, kbsample, kbconsumer, KB247003

↑ Back to the top

Article Info
Article ID : 247003
Revision : 6
Created on : 8/5/2004
Published on : 8/5/2004
Exists online : False
Views : 566