Esecuzione di modelli che contengono query SQL (provider SQLXMLOLEDB)
Questo esempio illustra l'uso della proprietà specifica del provider SQLXMLOLEDB ClientSideXML. In questa applicazione ADO di esempio sul lato client, viene eseguito un modello XML costituito da una query SQL nel server.
Poiché la proprietà ClientSideXML è impostata su True, l'istruzione SELECT senza la clausola FOR XML viene inviata al server. Il server esegue la query e restituisce un set di righe al client. Il client applica quindi la trasformazione FOR XML al set di righe e produce un documento XML.
Il modello XML fornisce un singolo elemento radice di primo livello (<ROOT>) per il documento XML generato, pertanto la proprietà radice xml non viene fornita.
Per eseguire il modello XML, è necessario specificare il sottolinguaggio {5d531cb2-e6ed-11d2-b252-00c04f681b71}.
Nota
Nel codice è necessario specificare il nome dell'istanza di SQL Server nella stringa di connessione. In questo esempio viene inoltre specificato l'uso del SQL Server Native Client (SQLNCLI11) per il provider di dati che richiede l'installazione di software client di rete aggiuntivo. Per altre informazioni, vedere Requisiti di sistema per 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;"
Set oTestCommand.ActiveConnection = oTestConnection
oTestCommand.Properties("ClientSideXML") = True
oTestCommand.CommandText = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql'> " & _
" <sql:query> " & _
" SELECT TOP 10 FirstName, LastName FROM Person.Contact FOR XML AUTO " & _
" </sql:query> " & _
" </ROOT> "
oTestStream.Open
' You need the dialect if you are executing
' XML templates (not for SQL queries).
oTestCommand.Dialect = "{5d531cb2-e6ed-11d2-b252-00c04f681b71}"
oTestCommand.Properties("Output Stream").Value = oTestStream
oTestCommand.Execute , , adExecuteStream
oTestStream.Position = 0
oTestStream.Charset = "utf-8"
Debug.Print oTestStream.ReadText(adReadAll)
End Sub
Sub Form_Load()
Main
End Sub