Dim obj_DataTable As New System.Data.DataTable("Category")
Dim obj_DataSet As New DataSet()
'Declaring the array of DataColum to hold the Primary Key Columns
Dim obj_PrimaryClmn(1) As System.Data.DataColumn
Dim obj_DataRow As System.Data.DataRow
Dim obj_DataRelation As DataRelation
Dim writer As New System.IO.StringWriter
obj_DataTable.Columns.Add(New System.Data.DataColumn("CategoryId"))
obj_DataTable.Columns.Add(New System.Data.DataColumn("CategoryName"))
obj_PrimaryClmn(0) = obj_DataTable.Columns(0)
'Assigning the CategoryId column as Primary Key
obj_DataTable.PrimaryKey = obj_PrimaryClmn
'Entering the data in Category Table
obj_DataRow = obj_DataTable.NewRow()
obj_DataRow.Item(0) = 0
obj_DataRow.Item(1) = "Select Category"
obj_DataTable.Rows.Add(obj_DataRow)
obj_DataRow = obj_DataTable.NewRow()
obj_DataRow.Item(0) = 1
obj_DataRow.Item(1) = "Computers"
obj_DataTable.Rows.Add(obj_DataRow)
obj_DataSet.Tables.Add(obj_DataTable)
obj_DataTable.PrimaryKey = obj_PrimaryClmn
Dim obj_DataRow1 As System.Data.DataRow
Dim obj_DataTable1 As New System.Data.DataTable("Product")
obj_DataTable1.Columns.Add(New System.Data.DataColumn("ProductId"))
obj_DataTable1.Columns.Add(New System.Data.DataColumn("ProductName"))
obj_DataTable1.Columns.Add(New System.Data.DataColumn("CategoryId"))
obj_DataRow1 = obj_DataTable1.NewRow()
obj_DataRow1.Item(0) = 1
obj_DataRow1.Item(1) = "PHP"
obj_DataRow1.Item(2) = 1
obj_DataTable1.Rows.Add(obj_DataRow1)
obj_DataSet.Tables.Add(obj_DataTable1)
Dim obj_ParentClmn, obj_ChildClmn As DataColumn
obj_ParentClmn = obj_DataSet.Tables("Category").Columns("CategoryId")
obj_ChildClmn = obj_DataSet.Tables("Product").Columns("CategoryId")
obj_DataRelation = New DataRelation("relation_Category_Product", obj_ParentClmn, obj_ChildClmn)
obj_DataSet.Relations.Add(obj_DataRelation)
obj_DataSet.WriteXml(writer)
MsgBox(writer.ToString())
此代码执行。父表和子表之间存在关系。此代码首先显示第一个xml和第二个表中的xml。我不需要这种方式。因为关系是存在的,xml应该在xml中显示第一个表,而将第二个表显示为第一个表的子节点。怎么可能?请帮帮我。
答案 0 :(得分:1)
根据以下代码将关系类型设置为Nested
,它将子表填充为XML中的子节点。
obj_DataRelation = New DataRelation("relation_Category_Product", obj_ParentClmn, obj_ChildClmn)
obj_DataRelation.Nested = True
obj_DataSet.Relations.Add(obj_DataRelation)
Refer here to know more about nesting of nodes, great knowledge