views:

79

answers:

2

I have a for xml raw select query that returns a xml string in server2005 and i would like to write that string to a file using vb.net.

A: 
Using strmWrite as System.IO.StreamWriter(filePath)
    strmWrite.WriteLine(xmlString)
End Using

Obviously, just fill in the filePath variable with the directory and name of your file. The xmlString variable will be your XML.

Sonny Boy
I know how to write the string to a file. I dont know how to get the string from sql. Sorry if i was not specific.
SamM
A: 

I found that if you fill a dataset with the result of the sql the first cell is the restult. How slow was i?!

----------Code Below-------

   Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim ds As New DataSet
        Dim sql As String
        Dim stringxml As String
        Dim SqlCon1 As String = "Data Source=SQLSERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;"
        connection = New SqlConnection(SqlCon1)
        sql = "select * from tblProduct for xml auto"
        Try
            connection.Open()
            adapter = New SqlDataAdapter(sql, SqlCon1)
            adapter.Fill(ds)
            connection.Close()
            stringxml = ds.Tables(0).Rows(0).Item(0)
            Using writer As StreamWriter = New StreamWriter("c:\testings\picktoday.xml")
                writer.Write(stringxml)
            End Using

            MsgBox("Done")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
SamM
Instead of using the DataAdapter you may want to make use of a DataReader instead. It's a bit more lightweight and suits your purposes better.You can then get the result out of the DataReader with some code similar to this:stringXML = reader(columnIndex).GetString()
Sonny Boy
yeh on further processing i found that there is a limit of 2033 chars when returning XML through SqlDataAdapter. SqlXmlAdapter was the way forward...
SamM