以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  两表比对的相关问题,谢谢  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=72074)

--  作者:zstk7333
--  发布时间:2015/7/22 23:10:00
--  两表比对的相关问题,谢谢
我想让表A中的第三列和表B的第二列进行比较,如果相同则pass,如果不同,新建一行,将不同的行复制到新建行。代码我这样写,为什么会出错,错在哪里了呢?请老师指教,谢谢!!

For b As Integer = 0 To DataTables("B").DataRows.Count-1

    For a As Integer = 0 To DataTables("A").DataRows.Count-1       

        If Tables("B").Rows(b)("第二列") <> Tables("A").Rows(a)("第三列") Then

            Tables("A").AddNew()

            Tables("A").Current("第三列") = Tables("B").Rows(b)("第二列")

        End If

    Next

Next

 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:管理项目1.rar


[此贴子已经被作者于2015/7/22 23:10:38编辑过]

--  作者:大红袍
--  发布时间:2015/7/22 23:23:00
--  

For Each dr As DataRow In DataTables("表B").DataRows
    Dim fdr As DataRow = DataTables("表A").Find("第三列 = \'" & dr("第二列") & "\'")
    If fdr Is Nothing Then
        fdr = DataTables("表A").AddNew()
        fdr("第三列") = dr("第二列")
    End If
Next

 


--  作者:zstk7333
--  发布时间:2015/7/22 23:24:00
--  回复:(大红袍)For Each dr As DataRow In DataTabl...
请问老师,我这样写为什么会出错呢?


--  作者:大红袍
--  发布时间:2015/7/22 23:33:00
--  

For b As Integer = 0 To DataTables("表B").DataRows.Count-1
    Dim contains As Boolean = False
    For a As Integer = 0 To DataTables("表A").DataRows.Count-1
       
        If Tables("表B").Rows(b)("第二列") =  Tables("表A").Rows(a)("第三列") Then
            contains = True
            Exit For
        End If
       
    Next
    If contains = False Then
        Tables("表A").AddNew()
        Tables("表A").Current("第三列") = Tables("表B").Rows(b)("第二列")
    End If
Next