VB.NET DataTable 数据合并:根据 AttribNo 匹配和赋值
VB.NET DataTable 数据合并:根据 AttribNo 匹配和赋值
本文将展示如何使用 VB.NET 代码将一个 DataTable1 的数据合并到另一个 DataTable2 中,并根据 'AttribNo' 属性进行匹配和赋值,将 DataTable1 中的 'AttribValue' 赋值给 DataTable2 中对应行的 'AttribValue'。
代码示例:
For Each row1 As DataRow In DataTable1.Rows
Dim attribNo As String = row1('AttribNo').ToString()
Dim matchingRows As DataRow() = DataTable2.Select('AttribNo = '' & attribNo & ''' )
For Each matchingRow As DataRow In matchingRows
matchingRow('AttribValue') = row1('AttribValue').ToString()
Next
Next
代码说明:
- 遍历 DataTable1 的每一行,获取 'AttribNo' 的值。
- 使用 'AttribNo' 的值在 DataTable2 中查询匹配的行,并将匹配的行存储在 'matchingRows' 数组中。
- 遍历 'matchingRows' 数组中的每一行,并将 DataTable1 中对应行的 'AttribValue' 值赋给 DataTable2 中的相应行的 'AttribValue'。
注意事项:
- 确保两个 DataTable 中都包含 'AttribNo' 和 'AttribValue' 列。
- 两个 DataTable 中的 'AttribNo' 列数据类型应该一致。
- 如果 DataTable2 中没有找到与 DataTable1 中 'AttribNo' 相匹配的行,则不会进行赋值操作。
其他方法:
除了使用循环和 Select 方法,还可以使用 LINQ 或 DataTable 的 Merge 方法来实现相同的功能。
希望本文能够帮助您了解如何在 VB.NET 中将 DataTable 数据进行合并和赋值。
原文地址: http://www.cveoy.top/t/topic/Z49 著作权归作者所有。请勿转载和采集!