手工编码填充数据

前面介绍了用Filler进行数据填充,Filler的设置很丰富,使用比较灵活。
不过填充数据并非一定要Filler,我们也可以手工编码填充。

例如需要从表A中,将金额大于100的记录,复制到表B中,假定两个表的结构一样:

For Each dr1 As DataRow In DataTables("表A").Select("[金额] > 100")
    Dim
dr2 As DataRow = DataTables("表B").AddNew()
   
For Each dc As DataCol in DataTables("表A").DataCols
        dr2(dc.Name) = dr1(dc.name)
   
Next
Next

如果表A和表B的结构不同,或者只需填充部分列,就需要定义数据来源列和数据接收列,例如:

Dim Cols1() As String = {"来源列一","来源列二","来源列三"}
Dim
Cols2() As String = {"接收列一","接收列二","接收列三"}
For Each
dr1 As DataRow In DataTables("表A").Select("[金额] > 100")
    Dim
dr2 As DataRow = DataTables("表B").AddNew()
    For
i As Integer = 0 To Cols1.Length -1
       
dr2(Cols2(i)) = dr1(Cols1(i
))
    Next
Next

如果要排除重复值,可以用GetValues获得不重复的值,然后再填充:

Dim Vals As List(of String())
Vals = DataTables("来源表").GetValues("产品|客户")
For
i As Integer = 0 To Vals.Count - 1
    Dim
dr As DataRow = DataTables("接收表").AddNew()
    dr(
"产品") = Vals(i)(0)
    dr(
"客户") = Vals(i)(1)
Next


本页地址:http://www.foxtable.com/webhelp/topics/1533.htm