此範例說明如何使用下列 SQLXMLOLEDB 提供者特定屬性:
ClientSideXMLBase PathMapping Schema
在此範例 ADO 應用程式中,會針對 XSD 對應架構指定 XPath 查詢 (root) (MySchema.xml)。 架構具有 <ContactID、FirstName 和 LastName 屬性的 Contacts> 元素。 在架構中,會進行預設對應:元素名稱會對應至具有相同名稱的數據表,而簡單類型的屬性會對應至具有相同名稱的數據行。
<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>
對應架構屬性會提供執行 XPath 查詢的對應架構。 對應架構可以是 XSD 或 XDR 架構。 [基底路徑] 屬性會提供對應架構的檔案路徑。
ClientSideXML 屬性設定為 True。 因此,XML 檔會在用戶端上產生。
在應用程式中,會直接指定 XPath 查詢。 因此,必須包含 XPath 方言 {ec2a4293-e898-11d2-b1b7-00c04f680c56} 。
備註
在程式代碼中,您必須在連接字串中提供 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") = 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