Udostępnij przez


Wykonywanie kwerendy XPath (Dostawca SQLXMLOLEDB)

Ten przykład ilustruje użycie następujących właściwości SQLXMLOLEDB specyficznych dla dostawca:

  • ClientSideXML

  • Base Path

  • Mapping Schema

W przykładowej aplikacji obiektów ADO kwerendę XPath (głównym) jest określona względem schematu mapowania XSD (MySchema.xml).Schemat ma <Kontakty> element o ContactID, Imię, and Nazwisko atrybuty.W schemacie domyślnego odwzorowania ma miejsce: Mapuje nazwę elementu do tabela z tą samą nazwą i atrybuty typu prostego mapy do kolumn o takich samych nazwach.

<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 = 'Contacts'/>
       </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name='Contacts' 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>

The Mapping Schema właściwość provides the mapping schema against which the XPath query is executed. Mapowanie schematu może być schematu XSD lub XDR.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 XPath {ec2a4293-e898-11 d 2-b1b7-00c04f680c56} muszą być włączone.

Uwaga

W kodzie musisz podać nazwę wystąpienie programu 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") = True

oTestCommand.CommandText = "root"
oTestStream.Open
oTestCommand.Dialect = "{ec2a4293-e898-11d2-b1b7-00c04f680c56}"
oTestCommand.Properties("Output Stream").Value = oTestStream
oTestCommand.Properties("Base Path").Value = "c:\Schemas\SQLXML4\XPathDirect\"
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