Microsoft Knowledge Base Email Alertz

(272180) - XMLTemplate.exe demonstrates how to extract an XML stream from a SQL Server 2000 database using an XML Template file and the ADO Command object.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 272180 - Last Review: August 5, 2004 - Revision: 5.5

SAMPLE: How to Retrieve XML Data Using a Template File with ADO in Visual C++

This article was previously published under Q272180

On This Page

SUMMARY

XMLTemplate.exe demonstrates how to extract an XML stream from a SQL Server 2000 database using an XML Template file and the ADO Command object.

MORE INFORMATION

The following file is available for download from the Microsoft Download Center:
XMLTemplate.exe (http://download.microsoft.com/download/mdac26/sample05/2.7/win98/en-us/xmltemplate.exe)
Release Date: Jan. 6, 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  (http://kbalertz.com/Feedback.aspx?kbNumber=119591/EN-US/ ) 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 XMLTemplate.exe file contains the following files:

Collapse this tableExpand this table
File nameSize
EULA.txt2 KB
Readme.txt4 KB
ADOHeader.h1 KB
Resource.h1 KB
Products.xml1 KB
Products.xsl3 KB
StdAfx.cpp1 KB
StdAfx.h2 KB
WebBrowser.cpp11 KB
WebBrowser.h4 KB
XMLTemplate.cpp3 KB
XMLTemplate.dsp5 KB
XMLTemplate.dsw1 KB
XMLTemplate.h2 KB
XMLTemplate.rc6 KB
XMLTemplateDlg.cpp8 KB
XMLTemplateDlg.h2 KB
\Res\XMLTemplate.ico2 KB
\Res\XMLTemplate.rc22 KB

This sample shows how an ADO client can run a SQL Server XML query stored in a template file and retrieve the XML data from a SQL Server 2000 database by using the Microsoft OLE DB Provider for SQL Server (SQLOLEDB) that ships with MDAC 2.6 or later.

The template file, Products.xml, contains the following parameterized SQL XML query:
<?xml version="1.0" ?> 
<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
   <sql:header>
      <sql:param name="ProdName">%</sql:param> 
   </sql:header>
   <sql:query>SELECT * FROM Products WHERE ProductName like '%' + @ProdName + '%' ORDER BY ProductName FOR XML AUTO</sql:query> 
</root>
This query uses the wildcard % to ensure that if no parameter is passed to it, the query is effectively reduced to a non-parameterized query.

The sample configures an input XML stream through an ADO Stream object, runs the SQL XML query through an ADO Command object, and then retrieves the output XML stream in an ADO Stream object. Depending on whether or not the XSL stylesheet Products.xsl is used for processing, the output XML stream the output files generated are:
  • Queryout.htm when you use XSL.

    -and-

  • Queryout.xml when you do not use XSL.


The sample does the following:
  1. Sets up a connection to a SQL Server 2000 database:

    Note The uid <user name> value must have the appropriate permissions to perform these operations on the database.
    _bstr_t m_Conn("PROVIDER=SQLOLEDB.1;Data Source=SQL2000Srv;Database=Northwind;uid=<user name>;pwd=<strong password>;");
  2. Configures the input Stream to load the template file Products.xml:
    hr = strmCmd->Open(vtEmpty,ado26::adModeUnknown,ado26::adOpenStreamUnspecified,L"",L"");
    strmCmd->PutCharset(L"ascii");
    strmCmd->PutType(ado26::adTypeText);
    hr = strmCmd->LoadFromFile(L"C:\\MyTemplateFilePath\\products.xml");
  3. Sets the ADO Command object's CommandStream property to the input Stream:
    hr = cmd->putref_CommandStream(strmCmd);
  4. Sets up an output Stream to retrieve the XML data:
    hr = cmd->Properties->Item[L"Output Stream"]->put_Value(_variant_t((IDispatch*) strmOutput));
  5. (Optional) Specifies the XSL file, Products.xsl, to process the XML data retrieved and output it in an HTML format:
    hr = cmd->Properties->Item[L"XSL"]->put_Value(_variant_t(L"products.xsl"));
  6. Runs the Command by using the adExecuteStream enumeration:
    hr = cmd->Execute(&vra,&vtEmpty,adExecuteStream);

Steps to Run the Sample

  1. Unzip XMLTemplate.exe.
  2. Modify the connection string in XMLTemplateDlg.cpp to refer to a valid SQL Server 2000 database.
  3. Compile and then run the application.

REFERENCES

271780  (http://kbalertz.com/Feedback.aspx?kbNumber=271780/EN-US/ ) SAMPLE: How To Retrieve XML Data using a SQL XML Query with ADO in Visual C++
271782  (http://kbalertz.com/Feedback.aspx?kbNumber=271782/EN-US/ ) SAMPLE: How To Retrieve XML Data Using an XPATH query with ADO in Visual C++
271775  (http://kbalertz.com/Feedback.aspx?kbNumber=271775/EN-US/ ) SAMPLE: HowTo Retrieve Relational Data Using OpenXML with ADO in Visual C++
Microsoft SQL Server 2000 Books OnLine

ADO 2.6 Documentation

APPLIES TO
  • Microsoft Data Access Components 2.6
  • Microsoft Data Access Components 2.7
  • Microsoft SQL Server 2000 Standard Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
Keywords: 
kbdownload kbfile kbinfo kbmsxmlnosweep KB272180
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate