SAMPLE: How to Retrieve Data Using a SQL XML Query in ATL OLE DB (272185)



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 Q272185

SUMMARY

OLEDBSQLXML.exe demonstrates how to create an ATL OLE DB client that extracts an XML Stream from a SQL 2000 Server using a SQL XML Query.

MORE INFORMATION

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

File nameSize
EULA.txt2 KB
OLEDBSQLXML.cpp4 KB

This sample requires the client to connect to a SQL Server 2000 Server using the Microsoft SQL Server OLE DB Provider (SQLOLEDB) that ships with Microsoft Data Access Components (MDAC) version 2.6 or later. Prior versions of SQL Server always returned data in the some form of recordset as a result of executing a 'SELECT..' SQL Statement. In SQL Server 2000, the 'SELECT ...' SQL Statement has been enhanced to include a new FOR XML clause, frequently called a SQL XML query. This clause allows SQL Server to return data in the form of an XML document.

In the SQLOLEDB provider that ships with MDAC 2.6, the ICommand::Execute method has been enhanced to return XML data using an ISequentialStream interface instead of IRowset. The SQL XML query can be executed by setting up the ICommandStream interface to contain the SQL XML query or by passing the query to the ICommandText::SetCommandText method. The sample does this as follows:
  1. Sets up the SQL XML query:
    m_szCommand = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql'>
    <sql:query>SELECT PRODUCTID, PRODUCTNAME FROM PRODUCTS WHERE PRODUCTNAME LIKE 'C%' FOR XML AUTO </sql:query>
    </ROOT>";
    					
  2. Sets the ICommandText interface to contain the SQL XML query set its dialect to DBGUID_MSSQLXML,
    if(FAILED(hr = m_spCommand->QueryInterface(&pCommandText)))
    {
      printf("Failed to get an ICommandStream interface...\n");
      return hr;
    }
    if(FAILED(hr = pCommandText->SetCommandText(DBGUID_MSSQLXML, T2COLE((LPCTSTR)m_szCommand))))
    {
      printf("Failed to set command stream.\n");
      return hr;
    }
    					
  3. Sets up an output stream to retrieve the XML data and execute the command:
    m_spCommand->Execute(NULL, IID_ISequentialStream, NULL, NULL, (IUnknown **) &pIXMLOutput );
  4. Executes the ADO Command using the adExecuteStream enumeration:
    hr = cmd->Execute(&vra,&vtEmpty,adExecuteStream);
    					

Steps to Run the Sample

  1. Create an empty Win32 console application.
  2. Insert OLEDBSQLXML.cpp into the project.
  3. Ensure that the project is built with the SQLOLEDB.H header file that ships with the MDAC 2.6 Software Development Kit (SDK).
  4. Modify the connection string to connect to a valid SQL Server 2000 database.
  5. Compile and then run the application.

Modification Type:MinorLast Reviewed:8/9/2004
Keywords:kbdownload kbDTL kbfile kbhowto kbMSXMLnosweep kbSample KB272185 kbAudDeveloper