Sdílet prostřednictvím


Executing Templates That Contain XPath Queries (SQLXMLOLEDB Provider)

This example shows how to use the following SQLXMLOLEDB Provider-specific properties:

  • ClientSideXML

  • Base Path

  • Mapping Schema

In this sample ADO application, an XML template that consists of an XPath query (root) is specified against the XSD mapping schema (MySchema.xml) that is described in Executing XPath Queries (SQLXMLOLEDB Provider).

The Mapping Schema property provides the XSD mapping schema against which the XPath query is executed. The Base Path property provides the file path to the mapping schema.

The ClientSideXML property is set to True. Therefore, the XML document is generated on the client.

In the application, an XPath query is specified directly. Therefore, the dialect {5d531cb2-e6ed-11d2-b252-00c04f681b71} must be included.

Note

In the code, you must provide the name of the instance of Microsoft SQL Server in the connection string. Also, this example specifies the use of the SQL Server Native Client (SQLNCLI10) for the data provider which requires additional network client software to be installed. For more information, see System Requirements for SQL Server 2008 R2 Native Client.

Option Explicit
Sub Main()

   Dim oTestStream As New ADODB.Stream
   Dim oTestConnection As New ADODB.Connection
   Dim oTestCommand As New ADODB.Command

   oTestConnection.Open "provider=SQLXMLOLEDB.4.0;data provider=SQLNCLI10;data source=SqlServerName;initial catalog=AdventureWorks2008R2;Integrated Security=SSPI;"

   oTestCommand.ActiveConnection = oTestConnection
   oTestCommand.Properties("ClientSideXML") = "False"

   oTestCommand.CommandText = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql'> " & _
        " <sql:xpath-query mapping-schema='mySchema.xml' > " & _
        "   root " & _
        "   </sql:xpath-query> " & _
        " </ROOT> "
   oTestStream.Open
   ' You need the dialect if you are executing a template.
   oTestCommand.Dialect = "{5d531cb2-e6ed-11d2-b252-00c04f681b71}"
   oTestCommand.Properties("Output Stream").Value = oTestStream
   oTestCommand.Properties("Base Path").Value = "c:\Schemas\SQLXML4\TemplateWithXPath\"
   oTestCommand.Properties("Mapping Schema").Value = "mySchema.xml"
   oTestCommand.Properties("Output Encoding") = "utf-8"
   oTestCommand.Execute , , adExecuteStream
   oTestStream.Position = 0
   oTestStream.Charset = "utf-8"
   Debug.Print oTestStream.ReadText(adReadAll)

End Sub

Sub Form_Load()
   Main
End Sub

This is the schema:

<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
   xmlns:sql='urn:schemas-microsoft-com:mapping-schema'>
 <xsd:element name= 'root' sql:is-constant='1'> 
    <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref = 'Contact'/>
       </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name='Contact' sql:relation='Person.Person'>
     <xsd:complexType>
          <xsd:attribute name='BusinessEntityID' type='xsd:integer' />
          <xsd:attribute name='FirstName' type='xsd:string'/> 
          <xsd:attribute name='LastName' type='xsd:string' /> 
     </xsd:complexType>
   </xsd:element>
</xsd:schema>