Foxtable(狐表)用户栏目专家坐堂 → 帮我看一下这个个代码错在哪里


  共有1707人关注过本帖树形打印复制链接

主题:帮我看一下这个个代码错在哪里

帅哥哟,离线,有人找我吗?
狐狸爸爸
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251060 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2016/3/18 8:34:00 [显示全部帖子]

这种业务逻辑的问题,应该自己分析。

 

先看你的逻辑:

 

当在“wztz”表内新增时"cljbqh_cphm"、“cljbqh_cpys"与"bmd"中有"cphm"、“cpys”进行比对,如果“bmd”表中有就不新增,反之没有就新增。

 

再看你的代码:

 

Dim i As Integer
For i  = DataTables("bmd").DataRows.Count-1 To 0 Step -1
    Dim dr As DataRow = DataTables("bmd").DataRows(i)
    Dim dr2 As DataRow = DataTables("wztz").Find("cljbqh_cphm='" & dr("cphm") & "' and cljbqh_cpys='" & dr("cpys") & "' and _Identify <> " & dr("_Identify"))
    If dr2 IsNot Nothing Then
        dr2.Delete
    End If
Next

 

逻辑很简单,但是这代码和逻辑能对上吗?哪里有逻辑中提到的新增呢?我看到的是“有就删除”,而不是“没有就增加”。

 

按照你的问题,不应该是这样的吗:

 

Dim i As Integer
For i  = DataTables("bmd").DataRows.Count-1 To 0 Step -1
    Dim dr As DataRow = DataTables("bmd").DataRows(i)
    Dim dr2 As DataRow = DataTables("wztz").Find("cljbqh_cphm='" & dr("cphm") & "' and cljbqh_cpys='" & dr("cpys") & "' and _Identify <> " & dr("_Identify"))
    If dr2 is Nothing Then '如果没有
        dr2 = DataTables("wztz").AddNew()  ‘就增加

        dr2("cljbqh_cphm") = dr("cphm")

        dr2("cljbqh_cpys") = dr("cpys")
    End If
Next

 

还有,两个不同的表,比较Identify是没有意义的,Identify是自动生成的,不需要比较,所以按照你的问题逻辑,应该:

 

Dim i As Integer
For i  = DataTables("bmd").DataRows.Count-1 To 0 Step -1
    Dim dr As DataRow = DataTables("bmd").DataRows(i)
    Dim dr2 As DataRow = DataTables("wztz").Find("cljbqh_cphm='" & dr("cphm") & "' and cljbqh_cpys='" & dr("cpys") & "'")
    If dr2 is Nothing Then '如果没有
        dr2 = DataTables("wztz").AddNew()  ‘就增加

        dr2("cljbqh_cphm") = dr("cphm")

        dr2("cljbqh_cpys") = dr("cpys")
    End If
Next

 

 

个人觉得,你原来代码中,其他比较不同表的Identify的代码,都应该去掉。

[此贴子已经被作者于2016/3/18 8:38:02编辑过]

 回到顶部