ADO 关系 2 个表,通过多于一个字段






1.60/5 (9投票s)
2004年4月7日

30945
通过多于一个字段关联 2 个表。
引言
通过 2 个或多个 System.Data.DataColumn 建立 2 个 System.Data.DataTable 之间的 System.Data.DataRelation 的方法。
在此示例中,您需要假设 DS_Geral 数据集中存在 2 个 DataTable 以及相应的字段...
Sub Ex()
Dim DR_Afectacao_Producao as System.Data.DataRelation
Dim Parent (1) as System.Data.datacolumn
Dim Child (1) as System.Data.datacolumn
'This arrays of datacolumns must be delimited by the number of fields
'conected in the relation,
'thei are 0-based
Parent (0) = DT_Afectacao.Columns("id_prod") 'The parent Table
Parent (1) = DT_Afectacao.Columns("id_xnuc") 'The parent Table
Child (0) = DT_Producao.Columns("id_prod") 'The child Table
Child (1) = DT_Producao.Columns("id_xnuc") 'The child Table
DR_Afectacao_Producao = new Datarelation ("Afect_Producao", Parent, Child, _
false )
DS_Geral.Relations.Add(DR_Afectacao_Producao)
End Sub
示例 1 获取子行
Dim ChildRows_1(), foundRow As DataRow
foundRow = DT_Afectacao.Rows.Find(Sender.Selecteditem.value)
ChildRows_1 = foundRow.GetChildRows("Afect_Producao")
结果: ChildRows_1 是所有子行的一个数组...示例 2 获取父行
Dim founded_ParentRow , foundRow As DataRow
foundRow = DT_Producao.Rows.Find(Sender.Selecteditem.value)
founded_ParentRow = foundRow.GetParentRow("Afect_Producao")
结果:foundParentRow 是关系中父 DataTable 中的一个 DataRow...摘要
“Rows.Find” 仅查找键值 ... 在 DataTable 的键列中。