2009年9月16日 星期三

DataSet序列化(M04)

1、使用DataSet內建方法
  • WriteXML方法:將DataSet寫成XML檔
  • ReadXML方法:將XML檔還原為DataSet
  • GetXML方法:取得序列化後的XML字串資料
2、控制DataSet序列化格式
  • 修改DataSet的DataSetName屬性
  • 修改DataTable的DataTable屬性
  • 修改DataColumn的ColumnName屬性
  • 修改DataColumn的ColumnMapping屬性
===================================
程式實作
Public Class Form1

Private Sub JobsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobsBindingNavigatorSaveItem.Click
Me.Validate()
Me.JobsBindingSource.EndEdit()
Me.JobsTableAdapter.Update(Me.PubsDataSet.jobs)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: 這行程式碼會將資料載入 'PubsDataSet.jobs' 資料表。您可以視需要進行移動或移除。
Me.JobsTableAdapter.Fill(Me.PubsDataSet.jobs)

End Sub
Public ds As New pubsDataSet
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripWriteDataSet.Click
Me.JobsTableAdapter.Fill(Me.ds.jobs)
ds.DataSetName = "工作列表"
For Each dc As DataColumn In ds.Tables(0).Columns
dc.ColumnMapping = MappingType.Attribute

Next
ds.Tables(0).Columns("job_id").ColumnName = "工作編號"
ds.WriteXml(Application.StartupPath & "\DataSet.xml")
End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripReadDataSet.Click
ds.Clear()
ds.ReadXml(Application.StartupPath & "\DataSet.xml")
MessageBox.Show("共讀取到" & ds.Tables(0).Rows.Count.ToString & "筆資料")
End Sub

Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripGetDataSet.Click
MessageBox.Show(ds.GetXml)
End Sub
End Class

沒有留言:

張貼留言