Friday, November 20, 2009
Home Retrieving XML Writing XML General Contact About  

How can I retrieve the results of a FOR XML query using VB? (33191 Requests)

Here is some sample code that I use to test my FOR XML queries. To run it follow these steps:

  1. Create a new EXE project in VB.
  2. Add references to MSXML 3.0 and ADO 2.6
  3. Copy the code below into your form's code. Be sure to change the connection string and put a real query in the sSQL variable.


Option Explicit

Const CONNSTRING = "<your connection string here>"

Private Sub Form_Load()

    Dim oCmd As Command
    Dim sSQL As String
    Dim oDom As IXMLDOMDocument2
   
    Set oDom = New DOMDocument30
   
    Set oCmd = New Command
    oCmd.ActiveConnection = CONNSTRING
    
   
    sSQL = "<root><sql:query xmlns:sql='urn:schemas-microsoft-com:xml-sql'>" & _
           "select * from table for xml auto</sql:query></root>"
     
    oCmd.CommandText = sSQL
    oCmd.Dialect = "{5D531CB2-E6Ed-11D2-B252-00C04F681B71}"
        
    oCmd.Properties("Output Stream") = oDom
    oCmd.Execute , , 1024
       
    oDom.Save "c:\temp\results.xml"
    
    Unload Me

End Sub

Other Resources:
HOWTO: Retrieve XML Data by Using a SQL XML Query in a Visual Basic Client

HOWTO: Retrieve XML Data with a Template File from a Visual Basic Client


Feedback

# How can I retrieve the results of a FOR XML query using VB? Brian Harrison

I used the exact code that you are asking for and the data does come out, because I have been able to pull it out of a stream if I send the output to a stream object, but everytime that I try to assign the data to a DOMDocument, I am unable to get any data from it. I can't assign the stream using the .load method nor can readtext the stream to a string variable and then use the .loadXML method. What could I be doing wrong?

# How can I retrieve the results of a FOR XML query using VB? Bryant

Sorry for the delayed reply. I didn't see your message until today. The code above should already load the data into a DOMDocument. The variable d (I really should make my names more descriptive) is a DOMDocument. So you shouldn't have to load the data into another variable. Send me an email if you still have questions. bryant@sqlxml.org.

# How can I retrieve the results of a FOR XML query using VB? ajoy

Thanks for your information. Can you explain me what is this {oCmd.Properties("Output Stream") } Is the Output Stream is key word or it is just any name. Thanks.

# How can I retrieve the results of a FOR XML query using VB? ajoy

Moreover i forgot to ask you how do you get this guid as "{5D531CB2-E6Ed-11D2-B252-00C04F681B71}" .Can we change this id.

# Error ajoy

I am getting an error as "Item cannot be found in the collection corresponding to the requested name or ordinal" at the statement "oCmd.Properties("Output Stream") = oDom". Can you please tell me what could be the problem

# Error Robert

Maybe you did not specify "Provider=SQLOLEDB" in your ConnectionString,and that does matter. Const CONNSTRING = "Provider=SQLOLEDB;Data Source=.;Initial Catalog=Northwind;User ID=SA;Password=;" Will this help?

# Returned XML using a Template Mark G

When using a template with for XML, nested to retrieve data, how do I know that the format will be constant? And if there is any change to the DB table structure the XSLT will no longer work. How is this eventuallity handled?

# re: How can I retrieve the results of a FOR XML query using VB? Mike Misconin

This reference is great but I would like to know how can I query a sql table to see if a record exists with http and report back to the client as a variable which can be used with vbscript.

I run a vbscript on the client wks to collect data. If the clients wks name does not exist in the workstation table then I create a update-gram which will post the data. If the workstation does exist, then I create a different update-gram which will post different data.

Can you help?

Mike Misconin
michael.misconin@nationalcity.com

# How can I retrieve data from the .xml file and how to give data to the same using VB6.? Raja

Sir, I don't have much knowledge on XML. Now i am studying. Could you please tell me the basic retrivel of data from XML using VB.
Please give the sample coding..
Thanks.

# Reg : How to get the xml structure and all datas from vb V.Sivakumar

Hi..

I read your article in the web "How can I retrieve the results of a FOR XML query using VB 7/24/2001 12:25 PM " it was very useful to me. I have a doubt reg. how to retrive the structure and data from xml through vb.

I need your help regarding this, i am expecting your valuable feedback to this mail id

unisoft_hr@rediffmail.com


Thanks
V.Sivakumar
Unisoft System Consultancy
INDIA.

# re: how to make a datareport in vb6 with parameters? joy

pls help....(beginner)

# re: How can I retrieve the results of a FOR XML query using VB? shirley

ineed my password to get my e-mail

# re: How can I retrieve the results of a FOR XML query using VB? tomy

select topic,author,max(timeofpost) as lastpost,count(contentid) as posts from forumdemo group by topic,author order by max(timeofpost)

# re: How can I retrieve the results of a FOR XML query using VB? Prakash T

How to create a xml file using query, the xml file contain a xsd file reference in vb 6?

# How can I retrieve the data from XML query using VBScript? Sivaprakasam S

I want to retrieve data from xml file by using the vbscript.

# lyxfn crpvams vobmywksu@mail.com

luxv nivkt fydokpj fuosxc wqzhiteb wmbphkrya srbempd

# vkio exbnf lnwxak@mail.com

ryqkmc dfwo rclyu dlqv cevwigj yjtwv zxawtmrg

# A name was started with an invalid character. A.Raj deep

Hi,

I am getting an error when i have query like this SELECT top 1 * FROM PRODDTA.F4801 a WHERE a.WASRST<> '8'.

The error message was

d:\public\XML_Generation3.vbs(16, 5) Microsoft XML Extensions to SQL Server: MSX
ML3: A name was started with an invalid character.

If anybody has experienced error message like this and got resolved ,pls reply.
Thank you,
Raj deep.A

# A name was started with an invalid character mark

I get the same error :(

# ptjx nafgv bqiwp@mail.com

mlihwzcta ahwfgbnvx ovscun wmxynp yfbqvpxwz wvcxqfubr vhyofzbul

# re: How can I retrieve the results of a FOR XML query using VB? Roskolnikov

I eat fish and speak suahilli

# re: How can I retrieve the results of a FOR XML query using VB? Michael A Klausmeyer

How do I retrieve the results of a dataview form and generate xml when I click the submit button?

# re: How can I retrieve the results of a FOR XML query using VB? Michael A Klausmeyer

How do I retrieve the results of a dataview form and generate xml when I click the submit button?

# re: How can I retrieve the results of a FOR XML query using VB? Michael

How do I retrieve the results of a dataview form and generate xml when I click the submit button?

# re: How can I retrieve the xml data using vb script? yathendra

how can extract the data from the xml file by creating the xmldom object and store it in
database

Title  
Name  
Url
Comments   


FAQ #1

last updated:
8/16/2001


Did the information in this faq help answer your question?





 
 

Survey Results: 194
Yes No N/A

© 2001, 2002, 2003 sqlxml.org