【问题标题】:Writing data from sql database in an xml file without dataset将sql数据库中的数据写入没有数据集的xml文件中
【发布时间】:2022-01-04 07:54:04
【问题描述】:

我想将 SQL 数据库中的数据写入 XML 文件。我知道可以使用数据集,但我不想使用它,因为我想用 XmlTextWriter 格式化我自己的 XML 文件。

我会给你一些参考。

使用我的连接字符串(名称、密码...),您可以构建一个新的 SqlConnection。然后我建立一个字符串 我的 SQL 查询在哪里。然后我打开连接,它可以工作。但我的问题是,我不知道如何在我的 XML 文件中写入查询的值。

connection = New SqlConnection(connetionString)

SQL 查询

Dim city As String = "SELECT City FROM info WHERE No = '1'"

编写我如何构建 XML 文件的代码。

Dim xmlfile As String = "path+name"
        If IO.File.Exists(xmlfile) = True Then
            IO.File.Delete(xmlfile)
        End If
        Dim enc As New System.Text.UTF8Encoding
       Dim XMLbg As New Xml.XmlTextWriter(xmlfile, enc)
        With XMLbg
            .Formatting = Xml.Formatting.Indented
            .Indentation = 4
            .WriteStartDocument()
            .WriteStartElement("Data")              
   --------------------------------------------------------
            .WriteElementString("City", **'here must be the Data for the City'** )
     
            .WriteEndElement() 'Data
            '--------------------------------------------------------
            XMLbg.Close()
        End With
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString, "Exception ", MessageBoxButtons.OK, MessageBoxIcon.Error)

也许有人知道怎么做。谢谢你:)

【问题讨论】:

    标签: sql xml database vb.net


    【解决方案1】:

    考虑使用实体框架。没有数据集,没有代码上的 SQL。

    【讨论】:

    • 这应该是一条评论。它没有回答问题。
    • 是的,但我没有 50 声望这样做
    【解决方案2】:

    好的,没有 DataSet,只有一个 DataTable。

    我不了解 XML 格式,所以我不确定 For Each 是属于 WriteStartElement...WriteEndElement 内部还是外部。我相信你知道它是如何工作的。

    Private Sub WriteXMLFile()
        Dim dt = GetDataForXMLFile()
        Dim xmlfile As String = "path+name"
        If IO.File.Exists(xmlfile) = True Then
            IO.File.Delete(xmlfile)
        End If
        Dim enc As New System.Text.UTF8Encoding
        Dim XMLbg As New Xml.XmlTextWriter(xmlfile, enc)
        With XMLbg
            .Formatting = Xml.Formatting.Indented
            .Indentation = 4
            .WriteStartDocument()
            For Each row As DataRow In dt.Rows
                .WriteStartElement("Data")
    
                .WriteElementString("City", Convert.ToString(row(0)))
    
                .WriteEndElement() 'Data
            Next
            XMLbg.Close()
        End With
    
    End Sub
    
    Private connectionString As String = "Your connection string"
    
    Private Function GetDataForXMLFile() As DataTable
        Dim dt As New DataTable
        Dim city As String = "SELECT City FROM info WHERE No = '1'"
        Using connection As New SqlConnection(connectionString),
            cmd As New SqlCommand(city, connection)
            Using reader = cmd.ExecuteReader
                dt.Load(reader)
            End Using
        End Using
        Return dt
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2016-07-03
      相关资源
      最近更新 更多