将xml原始查询的结果写入文件vb.net

时间:2009-11-24 14:51:56

标签: sql-server xml vb.net

我有一个for xml raw select查询,它在server2005中返回一个xml字符串,我想用vb.net将该字符串写入文件。

2 个答案:

答案 0 :(得分:2)

我发现如果用sql的结果填充数据集,则第一个单元格是restult。 我有多慢?!

----------下面的代码-------

   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

答案 1 :(得分:0)

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

显然,只需使用文件的目录和名称填写filePath变量即可。 xmlString变量将是您的XML。