如何在应用程序中保存数据?

时间:2014-06-14 05:20:24

标签: vb.net save

我正在创建一个基本的待办事项。我想保存用户创建的任务列表。现在我已经尝试将它们保存为文本文件,但我不希望这样。我希望它能够保存用户在程序中创建的任务而不是外部文本文件,然后检索这些保存的文件并将其显示在文本文件中。

基本上我需要一种方法来保存数据而不需要依赖数据库。

一个很好的例子是GeeTeeDee。它似乎是在程序内而不是外部文本文件中保存其文件和数据等。(我假设这是因为我似乎无法找到它们。我可能是错的)

更新

我正在做一些搜索可以遇到这个:Click here!!!

但问题是我对这是如何工作感到困惑。有人能为我清理事情吗?非常感谢它,因为它正是我正在寻找的。

1 个答案:

答案 0 :(得分:2)

"代码项目"示例将数据保存在扩展名为[* .brd]的外部文件中。

您可以使用XmlSerializer从扩展名为xml,brd或其他任何内容的外部xml文件中保存和加载数据。

尝试下面的代码,添加到form1三个按钮(Button1,Button2,Button3)和一个DataGridView1,粘贴代码并运行。

  1. 按下按钮"动态添加数据"或/和直接从DataGridView1添加,编辑,删除行。
  2. 按保存数据。
  3. 关闭并运行程序
  4. 按加载数据。
  5. Imports System.Xml.Serialization
    Imports System.IO
    Class Form1
        Dim ds As DataSet
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Button1.Text = "Load Data"
            Button2.Text = "add data dynamically"
            Button3.Text = "Save Data"
            'Create Dataset
            ds = CreateDataset()
            'Set DataGridView1 
            DataGridView1.DataSource = ds.Tables("Person")
        End Sub
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            LoadFromXMLfile("c:\temp\persons.xml")
        End Sub
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            AddDataToDataSetDynamically(ds)
        End Sub
        Private Sub Button3_Click_1(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            SaveToXMLFile("c:\temp\persons.xml", ds)
        End Sub
        Private Function CreateDataset() As DataSet
            Dim dataset1 As New DataSet("Persons")
            Dim table1 As New DataTable("Person")
            table1.Columns.Add("Id")
            table1.Columns.Add("FName")
            table1.Columns.Add("Age")
            '...
            dataset1.Tables.Add(table1)
            Return dataset1
        End Function
        Private Sub AddDataToDataSetDynamically(d As DataSet)
            d.Tables("Person").Rows.Add(1, "Andrew", "46")
            d.Tables("Person").Rows.Add(2, "Nicky", "43")
            d.Tables("Person").Rows.Add(3, "Helen", "15")
        End Sub
        Private Sub SaveToXMLFile(filename As String, d As DataSet)
            Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
            Dim writer As TextWriter = New StreamWriter(filename)
            ser.Serialize(writer, d)
            writer.Close()
        End Sub
        Private Sub LoadFromXMLfile(filename As String)
            If System.IO.File.Exists(filename) Then
                Dim xmlSerializer As XmlSerializer = New XmlSerializer(ds.GetType)
                Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
                ds = CType(xmlSerializer.Deserialize(readStream), DataSet)
                readStream.Close()
                DataGridView1.DataSource = ds.Tables("Person")
            Else
                MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
            End If
        End Sub
    End Class

    将该代码添加到 form1 并将数据导入文本框(添加button4,textbox1)

        Private Function PrintRows(dataSet As DataSet) As String
            Dim s As String = ""
            Dim thisTable As DataTable
            For Each thisTable In dataSet.Tables
                Dim row As DataRow
                For Each row In thisTable.Rows
                    Dim column As DataColumn
                    For Each column In thisTable.Columns
                        s &= row(column) & " "
                    Next column
                    s &= vbCrLf
                Next row
            Next thisTable
            Return s
        End Function
    
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            TextBox1.Text = PrintRows(ds)
        End Sub