2009-05-15 22 views
5

Mein Skript:Wie Sie XML-Daten von SQL Server 2005 abrufen?

Dim myStream, myConnection, myCommand 
Set myStream = CreateObject("ADODB.Stream") 
Set myConnection = CreateObject("ADODB.Connection") 
Set myCommand = CreateObject("ADODB.Command") 
' 
myConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;" & _ 
"Persist Security Info=False;Initial Catalog=DSIPAR;Data Source=.\DSIDATA" 

myCommand.ActiveConnection = myConnection 

myCommand.CommandText = "SELECT itemsgt, item FROM NIFItem" 

myStream.Open 

myCommand.Properties("Output Stream") = myStream 
myCommand.Execute , , adExecuteStream 

myStream.Position = 0 
myStream.Charset = "ISO-8859-1" 

Dim strxml 
strxml = myStream.ReadText 
MsgBox (strxml) 

kann ich das Skript ausführen, und ich kann die Abfrage ausführen, auf meinem SQL-Server-Instanz sehen, aber nichts ist jemals mit dem Ausgangsstrom zurückgeführt.

+0

Wie von Jose erwähnt, ruft Ihre SQL-Anweisung die Informationen nicht in Form eines XML ab. d. h. es verwendet FOR XML nicht – shahkalpesh

Antwort

5

Um die Ergebnisse in einem Stream abzurufen, muss die Abfrage "FOR XML AUTO" enthalten. Ändern Sie auch den Text adExecuteStream auf den konstanten Wert 1024.

Voll Code:

Dim myStream, myConnection, myCommand 
Set myStream = CreateObject("ADODB.Stream") 
Set myConnection = CreateObject("ADODB.Connection") 
Set myCommand = CreateObject("ADODB.Command") 
myConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;" & _ 
"Persist Security Info=False;Initial Catalog=DSIPAR;Data Source=.\DSIDATA" 
myCommand.ActiveConnection=myConnection 

myCommand.CommandText="SELECT itemsgt,item FROM NIFItem FOR XML AUTO" 

myStream.Open 
myCommand.Properties("Output Stream") = myStream 
myCommand.Properties("xml root") = "root" 

myCommand.Execute ,,1024 'instead of adExecuteStream 
myStream.Position=0 
myStream.Charset="ISO-8859-1" 
Dim strxml 
strxml = myStream.ReadText 
MsgBox (strxml) 

Wenn Sie keinen Strom benötigen und stattdessen wollen die Werte von itemsgt und Artikel lesen, versuchen Sie stattdessen:

Dim myStream, myConnection, myCommand, adoRec 
Set myStream = CreateObject("ADODB.Stream") 
Set myConnection = CreateObject("ADODB.Connection") 
Set myCommand = CreateObject("ADODB.Command") 
myConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;" & _ 
"Persist Security Info=False;Initial Catalog=DSIPAR;Data Source=.\DSIDATA" 
myCommand.ActiveConnection=myConnection 
myCommand.CommandText="SELECT itemsgt,item FROM NIFItem" 
SET adoRec = myCommand.Execute() 
'Get the first results 
If Not adoRec.EOF then 
    MsgBox "itemsgt = " & adoRec(0) & vbcrlf & "item=" & adoRec(1) 
End If 

'Iterate through the results 
While Not adoRec.EOF 
    itemsgt = adoRec(0) 
    item = adoRec(1) 
    'MsgBox "itemsgt = " & itemsgt & vbcrlf & "item=" & item 
    adoRec.MoveNext 
Wend 
+0

Der Grund, warum ich einen Stream möchte, ist, dass ich die Ausgabe nehme und sie an eine Nachrichtenwarteschlangennachricht sende. Mein ursprüngliches Skript verwendete die Konvention, die Sie für XML AUTO angegeben hatten, und funktionierte nicht. Ich dachte, ich würde Dinge vereinfachen, indem ich die XML-Formatierung aus dem Problem lösche. Ich werde Ihr vorgeschlagenes Recordset ausprobieren, um mir selbst zu beweisen, dass ich etwas zurückbekommen kann. –

+0

Ich habe gerade meinen Beitrag aktualisiert. Ein Teil von der XML AUTO fehlt, das andere Problem war Linie mit myCommand.Execute ,, adExecuteStream - ändern Sie das auf 1024 und es wird funktionieren. –

+0

Vielen Dank, es funktioniert gut mit den empfohlenen Änderungen. –