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.

SAMPLE: Using OLEDB Templates to Retrieve XML Data Through XPath Query


View products that this article applies to.

This article was previously published under Q272177

↑ Back to the top


Summary

The Oledbxpath.exe sample demonstrates how to use the ATL OLE DB consumer templates to extract an XML stream for a SQL Server 2000 Databases using an XPath query.

↑ Back to the top


More information

The following file is available for download from the Microsoft Download Center:
Release Date: 9-29-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 Oledbxpath.exe file contains the following files:

Collapse this tableExpand this table
File nameSize
EULA.txt2 KB
CustomerOrder.xml2 KB
OLEDBXPATH.cpp3 KB

This samples demonstrates how to run an XPath query to output an XML stream. The sample shows how to run an XPath query by using the OLE DB ICommandText interface. Alternatively, you can do this by using the ICommandStreaminterface that contains an input stream which in turn contains the XPath query.

Additionally, it provides a mapping schema file that contains mapping information between elements and attributes in the XPath query and the tables and columns contained in the back-end SQL Server 2000 relational database management system (RDBMS). The CustomerOrder.xml mapping schema file provided with this sample contains information regarding a SQL JOIN between the Customers and Orders tables and their columns in terms of the Customers and CustOrder elements and their attributes. The sample does the following:
  1. Sets the XPath query:
    m_szCommand = "/Customers/CustOrder[@CustomerID=\"ALFKI\"]";
    					
  2. Configures the Microsoft OLE DB Provider for SQL Server (SQLOLEDB) property set DBPROPSET_SQLSERVERSTREAM, which is used for configuring the input stream:
    		
    CDBPropSet	propset(DBPROPSET_SQLSERVERSTREAM);
    					
  3. Sets the Base Path property to the folder containing the mapping schema file:
    propset.AddProperty(SSPROP_STREAM_BASEPATH, (LPCWSTR)m_path);
    					
  4. Sets the Mapping Schema property to point to CustomerOrder.xml:
    propset.AddProperty(SSPROP_STREAM_MAPPINGSCHEMA, OLESTR("CustomerOrder.xml"));
    					
  5. Sets the dialect of ICommandText to DBGUID_XPATH:
    if(FAILED(hr = m_spCommand->QueryInterface(&pCommandText)))
    {
      printf("Failed to get an ICommandStream interface...\n");
      return hr;
    }
    if(FAILED(hr = pCommandText->SetCommandText(DBGUID_XPATH, T2COLE((LPCTSTR)m_szCommand))))
    {
      printf("Failed to set command stream.\n");
      return hr;
    }
    					
  6. Runs the ICommand::Execute method and retrieves the XML data in an output ISequentialStream interface:
    m_spCommand->Execute(NULL, IID_ISequentialStream, NULL, NULL, (IUnknown **) &pIXMLOutput );
    					

Steps to Run the Sample

  1. Create an empty Win32 console application.
  2. On the General tab of the Project/Settings dialog box, change the Microsoft Foundation Classes settings to Use MFC in a Static Library.
  3. Insert OLEDBXPath.cpp into the project.
  4. Copy CustomerOrder.xml into the project folder.
  5. Make sure that the project options point to the SQLOLEDB.H file that ships with Microsoft Data Access Components (MDAC) 2.6 SDK.
  6. Modify the connection string to refer to a valid SQL Server 2000 database.
  7. Compile and then run the application.

↑ Back to the top


References

271780� SAMPLE: How To Retrieve XML Data using a SQL XML Query with ADO in Visual C++
271775� SAMPLE: How To Retrieve Relational Data Using OpenXML in Visual C++
SQL Server 2000 Books OnLine

ADO 2.6 Documentation

↑ Back to the top


Keywords: KB272177, kbinfo, kbfile, kbdtl, kbdownload

↑ Back to the top

Article Info
Article ID : 272177
Revision : 9
Created on : 8/5/2004
Published on : 8/5/2004
Exists online : False
Views : 559