此示例演示如何使用以下 SQLXMLOLEDB 提供程序特定的属性:
ClientSideXML
基本路径
映射架构
在此示例 ADO 应用程序中,针对执行 XPath 查询(SQLXMLOLEDB 提供程序)中所述的 XSD 映射架构(MySchema.xml)指定由 XPath 查询(根)组成的 XML 模板。
映射架构属性提供执行 XPath 查询时所针对的 XSD 映射架构。 Base Path 属性提供映射架构的文件路径。
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>