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



The information in this article applies to:

  • Microsoft Data Access Components 2.6
  • Microsoft Data Access Components 2.7
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q272177

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.

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:

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.

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++

%1 %2

SQL Server 2000 Books OnLine

ADO 2.6 Documentation

Modification Type:MinorLast Reviewed:8/5/2004
Keywords:kbdownload kbDTL kbfile kbinfo KB272177