这种业务逻辑的问题,应该自己分析。
先看你的逻辑:
当在“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编辑过]