Microsoft KB Archive/271621

= How To Retrieve XML Data with a Template File from a Visual Basic Client =

Article ID: 271621

Article Last Modified on 11/22/2005

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q271621



SUMMARY
This sample in this article demonstrates how to retrieve an XML document from SQL Server 2000 by using a template file that contains parameters. More specifically, a query is issued against SQL Server 2000 by using an ActiveX Data Objects (ADO) 2.6 command stream. This query fetches the XML DataStream.

This sample reads the &quot;products.xml&quot; template file from disk and sets the command text for the ADODB.Command object for the query to run.

You must set the following properties for the Properties collection of the ADODB.Command object:
 * Base Path. This property establishes the location of the template and the Extensible Style Sheet Language (XSL) files.
 * Output Stream. This property designates where the resulting XML data stream will be piped.
 * Dialect. The dialect defines the syntax and the general rules that the provider uses to parse the string or the stream. By setting the command language dialect, you specify how the Microsoft OLE DB Provider for SQL Server interprets the command text that is received from ADO. The dialect is specified by a globally unique identifier (GUID) and is set by using the Dialect property of the Command object.
 * XSL. This property transforms the XML document.

NOTE: Output Stream may be any object that supports an IStream or ISequentialStream interface. Objects that support the IStream interfaces are the ADODB.Stream object, the Microsoft Internet Information Services (IIS) 5.0 Response object, and a Microsoft XML (MSXML) DomDocument object.



MORE INFORMATION
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click Components, and then add a reference to both Microsoft Internet Controls and Microsoft ActiveX Data Objects 2.6. Add a Web browser to the form, name it WebBrowser1, and then size it accordingly. Add a frame to the form, and place two option buttons in it. Name the first button optXSLYes with the caption XSL Yes, and then name the second button optXSLNo with the caption XSL No. Add two command buttons. Name the first button cmdTestIt with the caption Test, and then name the second button cmdExitProgram with the caption Exit.  Paste the following Visual Basic code in the Code window of the form: Option Explicit Const DBGUID_DEFAULT As String = &quot;{C8B521FB-5CF3-11CE-ADE5-00AA0044773D}&quot; Private Sub cmdExitProgram_Click Unload Me   End End Sub

Private Sub cmdTestIt_Click Dim cn As New ADODB.Connection Dim cmd As New ADODB.Command Dim cmdStream As New ADODB.Stream Dim cmdOutput As New ADODB.Stream Dim txtOutputFileName As String ' open the database connection cn.Open &quot;provider=sqloledb;data source=.;initial catalog=northwind;uid=sa;pwd=&quot; ' open the command stream that will eventually contain the templated query cmdStream.Open ' set the character set to ascii cmdStream.Charset = &quot;ascii&quot; ' set the command stream type to text, not binary. cmdStream.Type = adTypeText ' read the template file from disk into the command stream to execute cmdStream.LoadFromFile App.Path & &quot;\products.xml&quot; ' set the command connection Set cmd.ActiveConnection = cn   ' set the command's command stream to hook the template query to the command you want to run. Set cmd.CommandStream = cmdStream ' set the command dialect cmd.Dialect = DBGUID_DEFAULT ' open the output stream to receive the results for the command execute. cmdOutput.Open ' set the base path for where the template file resides. ' Currently, this must point to a file on disk. Remote templates via ' http://...template.xml&quot; are not allowed. cmd.Properties(&quot;Base Path&quot;) = App.Path ' set up the output stream that will receive the output of the command execute. cmd.Properties(&quot;Output Stream&quot;) = cmdOutput ' set the XSL to process if the user requested the output to be fixed. If optXSLYes Then ' set the file name for the XSL: this inherits the &quot;base path&quot; setting cmd.Properties(&quot;XSL&quot;) = &quot;products.xsl&quot; ' set the file extension to &quot;.htm&quot;, mainly so the Web browser displays ' set the output as a table. Try with a &quot;.xml&quot; extension... what is displayed? txtOutputFileName = App.Path & &quot;\queryout.htm&quot; Else txtOutputFileName = App.Path & &quot;\queryout.xml&quot; End If

' execute the command stream with the settings specified above. cmd.Execute, , adExecuteStream ' position the stream back to the beginning: the &quot;file position&quot; in the 'stream will be at the end of the stream. Writing to a stream will append 'on the end and only while the output from the command execute generates. cmdOutput.Position = 0 'save the output to a file, this is only needed to use the navigate on the Web browser control cmdOutput.SaveToFile txtOutputFileName, adSaveCreateOverWrite ' Navigate/display the results of the command executes. WebBrowser1.Navigate txtOutputFileName ' close and clean-up the objects used. cmdOutput.Close cmdStream.Close cn.Close Set cmdOutput = Nothing Set cmdStream = Nothing Set cmd = Nothing Set cn = Nothing End Sub   To create a template file, create a new text file, paste the following template into the file, and then save it with a name of products.xml: <?xml version='1.0' ?> <root xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;> <sql:header> <sql:param name=&quot;ProdName&quot;>%</sql:param> </sql:header> <sql:query> SELECT     * FROM       Products WHERE      ProductName like '%' + @ProdName + '%' ORDER BY   ProductName FOR XML AUTO </sql:query> </li>  As an option, create a file named products.xsl with the following code to transform the output: <?xml version='1.0' ?> <xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/TR/WD-xsl&quot;> <xsl:template match=&quot;/&quot;> MSDN ADO Product Sample with SQL Server 2000 Features <base href=&quot;http://localhost/3tier/&quot; /> <table border=&quot;0&quot; cellPadding=&quot;1&quot; cellSpacing=&quot;1&quot; width=&quot;100%&quot; style=&quot;COLOR:black;FONT-FAMILY:Arial;FONT-SIZE:12pt.;FONT-WEIGHT:500&quot;>

<tr bgColor=&quot;#336699&quot; align=&quot;center&quot;> <TD><P ><STRONG><FONT color=&quot;white&quot; size=&quot;2&quot;>Product ID:</FONT></STRONG></P></TD> <TD><P ><STRONG><FONT color=&quot;white&quot; size=&quot;2&quot;>Product Name:</FONT></STRONG></P></TD> <TD><P ><STRONG><FONT color=&quot;white&quot; size=&quot;2&quot;>Unit Price:</FONT></STRONG></P></TD> <TD><P ><STRONG><FONT color=&quot;white&quot; size=&quot;2&quot;>Units In Stock:</FONT></STRONG></P></TD> <TD><P ><STRONG><FONT color=&quot;white&quot; size=&quot;2&quot;>Restock Level:</FONT></STRONG></P></TD> <TD><P ><FONT color=&quot;white&quot; size=&quot;2&quot;><STRONG>Units On Order:</STRONG></FONT></P></TD> <xsl:for-each select=&quot;root/Products&quot;> <tr style=&quot;COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 0.8em; FONT-WEIGHT: 500&quot;> <td bgColor=&quot;#F0F0F0&quot;><xsl:value-of select=&quot;@ProductID&quot;/> <td bgColor=&quot;#F0F0F0&quot;><xsl:value-of select=&quot;@ProductName&quot;/> <td bgColor=&quot;#F0F0F0&quot;><xsl:value-of select=&quot;@UnitPrice&quot;/> <td bgColor=&quot;#F0F0F0&quot;><xsl:value-of select=&quot;@UnitsInStock&quot;/> <td bgColor=&quot;#F0F0F0&quot;><xsl:value-of select=&quot;@ReorderLevel&quot;/> <td bgColor=&quot;#F0F0F0&quot;><xsl:value-of select=&quot;@UnitsOnOrder&quot;/> </xsl:for-each> </xsl:template> </xsl:stylesheet> </li></ol>

<div class="references_section">