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

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

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

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

按下按钮"add data dynamically"或/并直接从DataGridView1添加,编辑,删除行 .

按保存数据 .

关闭并运行程序

按加载数据 .

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值