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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24216835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:41:29  来源:igfitidea点击:

How to save data within the Application?

vb.netsave

提问by Zer0

I'm creating a basic To-do program. I want to save the list of tasks the user creates. Now I've tried with saving them as text files but I don't want it that way. I want it so that I can save the tasks the user creates within the program rather than external text files and then retrieve those saved files and display them in a text file.

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

Essentially I need a way to save data without needing to rely on databases.

本质上,我需要一种无需依赖数据库即可保存数据的方法。

A good example is GeeTeeDee. It seems to be saving its files and data etc. in the program within rather than external text file.(I'm assuming this because I can't seem find them. I could be wrong)

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

Update

更新

I was doing a bit of searching can came across this: Click here!!!

我正在做一些搜索可以发现这个:点击这里!!!

But the problem is that I'm confused as to how this works. Is someone able to clear things for me? It would be GREATLY appreciated as it's exactly what I'm looking for.

但问题是我对这是如何工作的感到困惑。有人能帮我清理事情吗?将不胜感激,因为这正是我正在寻找的。

回答by ooopsoft

The "code project" example saves the data at an external file with extension [*.brd] .

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

You can Use XmlSerializer to save and load your data from external xml file with extension xml,brd or anything else.

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

Try the code below, add into a form1 three buttons (Button1,Button2,Button3) and a DataGridView1, paste the code and Run.

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

  1. press button "add data dynamically" or/and add,edit,delete row directlly from DataGridView1.
  2. press Save data.
  3. close and run programm
  4. press Load data.
  1. 按下按钮“动态添加数据”或/和直接从 DataGridView1 添加、编辑、删除行。
  2. 按保存数据。
  3. 关闭并运行程序
  4. 按加载数据。
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

add that code to form1and get the data to a textbox (add button4,textbox1)

将该代码添加到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