1

我想提出这个问题....如果循环中只有 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 个错误

4

2 回答 2

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
于 2014-03-18T06:32:01.013 回答
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
于 2014-03-18T06:41:35.843 回答