我想提出这个问题....如果循环中只有1个假,那么它将不会插入到数据库中,反之亦然,如果全部为真,那么它将插入数据库。
这是我的代码
Public Function Check_Foreign_Key(ByVal select_column_name As String, ByVal table_name As String, ByVal where_column_name As String, ByVal where_value As String, ByRef Foreign_Key As String) As Boolean
Dim dt_service_provider_id As DataTable = ExecuteSQLQuery("select " & select_column_name & " from " & table_name & " where " & where_column_name & " = '" & where_value & "'")
If dt_service_provider_id.Rows.Count = 0 Then
Return False
Else
Foreign_Key = dt_service_provider_id.Rows(0).Item(0).ToString()
Return True
End If
End Function
For Each dr As DataRow In dt.Rows
Dim dt_rows as Integer = 0
Dim site_id As Boolean = Check_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString, dt_fk)
If dr(0).ToString.Length > 50 And dr(0).ToString = "" And dr(0).ToString Is Nothing Then
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, "Character is greater than 50")
'in here i want to continue checking go to next, i think i got error in cheking if dr(0) is empty
Else
If dr(1).ToString.Length > 50 And dr(1).ToString = "" And dr(1).ToString Is Nothing Then
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, "Character is greater than 50")
'in here i want to continue again to next
Else
Select Case site_id
Case False
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, dr(2).ToString)
Return
End Select
End If
End If
dt_row += 1
Next
这是我唯一知道的代码但不幸的是我无法得到我的逻辑它总是插入而不是它有1个错误
答案 0 :(得分:1)
试试这个
Private SaveToDB()
For Each dr As DataRow In dt.Rows
Dim site_id As Boolean = Check_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString, dt_fk)
Select Case site_id
Case False
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, dr(2).ToString)
Return
End Select
Next
'if you got here means no error
'NOTE: You should loop again and save the rows
For Each dr As DataRow In dt.Rows
'Your code to save into the database here
Next
End Sub
答案 1 :(得分:1)
希望这可以解决问题
Dim blnAllTrue As Boolean = True
For Each dr As DataRow In dt.Rows
Dim site_id As Boolean = Check_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString, dt_fk)
blnAllTrue = blnAllTrue And site_id
If site_id = False Then
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, dr(2).ToString)
End If
Next
If blnAllTrue = True Then
'insert into your database
End If