「code project」示例將數據保存在擴展名爲[* .brd]的外部文件中。
您可以使用XmlSerializer從外部XML文件(擴展名爲xml,brd或其他)中保存和加載數據。
嘗試下面的代碼,添加到form1三個按鈕(Button1,Button2,Button3)和DataGridView1中,粘貼代碼並運行。
- 按下按鈕「動態添加數據」或/和直接從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
你必須從某個地方存儲數據 - 無論是文本文件,數據庫或一些其他的持久存儲機制。一旦程序終止,內存中的任何數據都將丟失。 – Tim
我同意Tim的觀點,在我看來,如果沒有保存在純文本文件中,你可以將它們保存到xml文件中,使用'xml serailization'讀取和寫入更加方便,或者你可以將值存儲到'registry' –
你無法找到這些文件並不意味着他們沒有這樣做(程序不寫入EXE)。 [此鏈接可能會解釋](http://msdn.microsoft.com/en-us/library/ms379611.aspx)爲什麼你無法找到它們。 My.Settings是一種選擇。 [這個答案](http://stackoverflow.com/a/23659023/1070452)顯示瞭如何序列化一個小的中等數據集的類對象的列表。具體的答案取決於你準確保存的內容。 – Plutonix