此範例示範如何使用下列 SQLXMLOLEDB 提供者特定屬性:
ClientSideXML
基礎路徑
對應架構
在此範例 ADO 應用程式中,會針對 執行 XPath 查詢(SQLXMLOLEDB 提供者)中所述的 XSD 對應架構(MySchema.xml)指定由 XPath 查詢(root) 組成的 XML 範本。
對應架構屬性會提供執行 XPath 查詢的 XSD 對應架構。 [基底路徑] 屬性會提供對應架構的檔案路徑。
ClientSideXML 屬性設定為 True。 因此,XML 檔會在用戶端上產生。
在應用程式中,會直接指定 XPath 查詢。 因此,必須包含方言 {5d531cb2-e6ed-11d2-b252-00c04f681b71} 。
備註
在程式代碼中,您必須在連接字串中提供 Microsoft SQL Server 實例的名稱。 此外,此範例會指定數據提供者使用 SQL Server Native Client (SQLNCLI11),而數據提供者需要安裝額外的網路客戶端軟體。 如需詳細資訊,請參閱 SQL Server 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=SQLNCLI11;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
這是架構:
<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>