重新整理:
'引用判断.条件是判断的列名必须相同.
Dim bm1 As String = "表A" '删除行所在表
Dim lm As String = "第一列" '以该列为依据,判断是否被引用
Dim lm2,hhz As String
Dim bm2 As String = Tables(bm1).DataTable.name
For Each dt As DataTable In DataTables '遍历各表
If DataTables(dt.Name).DataCols.Contains(lm) Then '包含指定列
If Tables(bm1).BottomPosition > -1 And Tables(bm1).Rows(Tables(bm1).BottomPosition).isnull(lm) = False Then '存在指定行
For i As Integer = Tables(bm1).BottomPosition To Tables(bm1).TopPosition Step -1
Dim dr As Row = Tables(bm1).Rows(i)
Dim d As String = lm & " = '" & dr(lm) & "'"
Dim d2 As String = "count(" & lm & ")"
Dim d1 As String
Dim r As Integer = eval(dt.Compute(d2,d))
If bm2 = dt.name Then
If cdec(r) > 1 Then
d1 = dt.Find(d & " And " & lm & " Is not null")(lm)
hhz = """" & dr(lm) & """" & "已经被" & """" & dt.name & """引用,禁止删除!"
Return hhz
Exit For
End If
Else
If cdec(r) > 0 Then
d1 = dt.Find(d & " And " & lm & " Is not null")(lm)
hhz = """" & dr(lm) & """" & "已经被" & """" & dt.name & """引用,禁止删除!"
Return hhz
Exit For
End If
End If
Next
End If
End If
If hhz IsNot Nothing Then
Return hhz
End If
Next
Dim cmd As New SQLCommand
'cmd.C 指定数据源
For Each dt As DataTable In DataTables
If dt.name IsNot Nothing And DataTables(dt.name).Type <> 4 And DataTables(dt.name).Type <> 5 Then
If DataTables(dt.name).DataCols.Contains(lm) Then
If Tables(bm1).BottomPosition > -1 Then
For i As Integer = Tables(bm1).BottomPosition To Tables(bm1).TopPosition Step -1
Dim lz As String = Tables(bm1).Rows(i)(lm)
Dim nm As String = "Select Count(*) From {" & dt.name & "} Where " & lm & " = " & "'" & lz & "'"
cmd.CommandText = nm
If bm2 = dt.name Then
If cmd.ExecuteScalar > 1 Then
hhz = """" & Tables(bm1).Rows(i)(lm) & """已被""" & dt.name & """引用,禁止删除!" & vbcrlf & "如果你确认该条记录已经更改或删除,请先保存以便及时更新后台数据,再进行相应操作!"
Return hhz
Exit For
End If
Else
If cmd.ExecuteScalar > 0 Then
hhz = """" & Tables(bm1).Rows(i)(lm) & """已被""" & dt.name & """引用,禁止删除!" & vbcrlf & "如果你确认该条记录已经更改或删除,请先保存以便及时更新后台数据,再进行相应操作!"
Return hhz
Exit For
End If
Exit For
End If
Next
End If
End If
End If
If hhz IsNot Nothing Then
Return hhz
Exit For
End If
Next