Microsoft KB Archive/271780

= SAMPLE: How To Retrieve XML Data Using an SQL XML Query with ADO in Visual C++ =

Article ID: 271780

Article Last Modified on 8/9/2004

-

APPLIES TO


 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft SQL Server 2000 Standard Edition

-



This article was previously published under Q271780



SUMMARY
XMLStream.exe demonstrates how to create an ActiveX Data Objects (ADO) client that uses an SQL XML query to extract an XML stream from a SQL Server 2000 server.



MORE INFORMATION
This sample requires the client to connect to a SQL Server 2000 server by using the Microsoft OLE DB Provider for SQL Server that ships with Microsoft Data Access Components (MDAC) version 2.6 or later only. Prior versions of SQL Server always returned data in some form of a 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 an SQL XML query. This clause allows SQL Server to return data in the form of an XML document.

In addition, the ADO Command object has been enhanced to configure and retrieve XML data as a result of executing &quot;SELECT ... FOR XML&quot; SQL queries.

XMLStream.exe provides an example that does the following:   Sets up the connection to a SQL Server 2000 database: _bstr_t m_Conn(&quot;PROVIDER=SQLOLEDB.1;Server=SQL2000Srv;Database=Northwind;uid=sa;pwd=;&quot;);   Sets up the SQL XML query: _bstr_t m_Qry(&quot;             SELECT * FROM PRODUCTS FOR XML AUTO              &quot;);   Configures the input Stream object to contain the SQL XML query: hr = strm->Open(vtEmpty,adModeUnknown,adOpenStreamUnspecified,L&quot;&quot;,L&quot;&quot;); hr = strm->WriteText(m_Qry,adWriteChar);   Sets the CommandStream property of the ADO Command object to the input Stream: hr = cmd->putref_CommandStream(strm);   Assigns the SQL XML globally unique identifier (GUID) to the Dialect property of the Command object:

NOTE: Dialect defines the syntax and general rules that the provider uses to parse the string or stream. Setting the command language dialect specifies how the Microsoft OLE DB Provider for SQL Server interprets the command text that is received from ADO. hr = cmd->put_Dialect(_bstr_t(&quot;{5D531CB2-E6Ed-11D2-B252-00C04F681B71}&quot;)); </li>  Sets up an output Stream to retrieve the XML data: hr = cmd->Properties->Item[L&quot;Output Stream&quot;]->put_Value(_variant_t((IDispatch*) outStrm)); </li>  Executes the Command by using the adExecuteStream enumeration: hr = cmd->Execute(&vra,&vtEmpty,adExecuteStream); </li></ol>

Steps to Run the Sample

 * 1) Unzip XMLStream.exe.
 * 2) Create an empty Win32 console application.
 * 3) Insert the XMLStream.cpp file into the project.
 * 4) Modify the connection string to refer to a valid SQL Server 2000 database.
 * 5) From the Project menu, click Settings, and then click the C/C++ tab. In the Project Options box, change /MLd to /MTd.
 * 6) Compile and then run the application.

The following file is available for download from the Microsoft Download Center:

XMLStream.exe

Release Date: September 27, 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 XMLStream.exe file contains the following files:

<div class="references_section">