tags:

views:

1120

answers:

2

Using a variety of sources I put together the following script ...

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"
MsgBox(myConnection.State)
MsgBox(myConnection.Provider)
MsgBox(myConnection.Version)
myCommand.ActiveConnection=myConnection
myCommand.CommandText="SELECT itemsgt,item FROM NIFItem"
MsgBox(myCommand.CommandText)
myStream.Open
myCommand.Properties("Output Stream") = myStream
'myCommand.Properties("xml root") = "root"
myCommand.Execute ,,adExecuteStream
myStream.Position=0
MsgBox(myStream.Charset)
MsgBox(myStream.Size)
MsgBox(myStream.Type)
myStream.Charset="ISO-8859-1"
Dim strxml
strxml = myStream.ReadText
MsgBox (strxml)

Sorry about all the message box but I am trying to debug ... anyway I can run the script, I can see the query execute on my SQL server instance, but nothing is ever returned to the output stream. I have checked the code multiple times and see nothing wrong.

Thanks for any ideas.

A: 

Have you tried to debug the vbs from cmd prompt?
Run the script like this:
cscript //X yourscript.vbs

Kb
Thanks for pointing out this choice when run in the debug setting (visual studio 2005 is the debugger) I see no evidence of any change in response to the myCommand.Properties("Output Stream") statement. It is almost as if myCommand does not have such a property for setting (or at least it does not show up in the debugger window).
+2  A: 

To retrieve the results into a stream, the query needs to include "FOR XML AUTO". Also change the text adExecuteStream to the constant value 1024.

Full 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)

If you don't need a stream and instead want to read the values of itemsgt and item, try this instead:

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
Jose Basilio
The reason I am wanting a stream is that I am taking the output and sending it on to a message queue message. My original script used the convention you indicated for XML AUTO and did not work, I thought I was simplifying things by remove XML formatting from the problem. I will try your suggested recordset just to prove to myself I can get something back.
I just updated my post. A part from the XML AUTO missing, the other issue was line with myCommand.Execute ,,adExecuteStream -- change that to 1024 and it will work.
Jose Basilio
Many thanks, it works fine with the recommended changes.