Udostępnij przez


Wykonywanie szablony zawierające kwerendy XPath (Dostawca SQLXMLOLEDB)

W tym przykładzie pokazano, jak użyć następujących właściwości SQLXMLOLEDB specyficzne dla dostawca:

  • ClientSideXML

  • Base Path

  • Mapping Schema

W przykładowej aplikacji obiektów ADO szablonu XML, który składa się z kwerendę XPath (głównym) jest określona względem schematu mapowania XSD (MySchema.xml) opisaną w Wykonywanie kwerendy XPath (Dostawca SQLXMLOLEDB).

The Mapping Schema właściwość provides the XSD mapping schema against which the XPath query is executed. The Base Path właściwość provides the file ścieżka to the mapping schema.

The ClientSideXML właściwość is zestaw to True. Dlatego dokument XML jest generowany na komputerze klienckim.

W aplikacji jest określona bezpośrednio kwerendę XPath.Dlatego też dialekt {5d531cb2-e6ed-11 d 2-b252-00c04f681b71} muszą być włączone.

Uwaga

W kodzie musisz podać nazwę wystąpienie programu Microsoft SQL Server w ciąg połączenia. Ponadto w tym przykładzie określa używanie SQL Server Macierzysty klient (SQLNCLI10) dla dostawca danych, który wymaga oprogramowanie klient sieciowego dodatkowe mają być zainstalowane. Aby uzyskać więcej informacji zobaczWymagania systemowe dla programu SQL Server 2008 Native klient.

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=AdventureWorks;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

Jest to schemat:

<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.Contact'>
     <xsd:complexType>
          <xsd:attribute name='ContactID' type='xsd:integer' />
          <xsd:attribute name='FirstName' type='xsd:string'/> 
          <xsd:attribute name='LastName' type='xsd:string' /> 
     </xsd:complexType>
   </xsd:element>
</xsd:schema>