write result of for xml raw query to file vb.net
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 fi开发者_JAVA技巧le using vb.net.
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
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.
I just want to add to the accepted answer when we have a long output we can get data in multiple rows so we can use the following code
dim xmloutput as string
For Each item As DataRow In ds.Tables(0).Rows
xmloutput += item.Item(0)
Next
精彩评论