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

ID: Q247003


The information in this article applies to:
  • Microsoft Data Access Components version 2.1
  • Microsoft Visual C++, 32-bit Editions, version 6.0


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.


MORE INFORMATION

The following file is available for download from the Microsoft Download Center. Click the file name below to download the file:

ATLShape.exe
Release Date:

For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.

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;
    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:


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


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


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


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


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


    12. 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:

    Keywords : kbfile kbsample kbATL210 kbDatabase kbOLEDB210 kbVC600 kbGrpVCDB kbDSupport
    Version : WINDOWS:2.1; winnt:6.0
    Platform : WINDOWS winnt
    Issue type : kbinfo


    Last Reviewed: January 14, 2000
    © 2000 Microsoft Corporation. All rights reserved. Terms of Use.