Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to continue insert if one row fails
- Dim WrongValuedLinesList As New List(Of String)
- Dim ConversionFailedList As New List(Of String)
- Dim InsertionFailedList As New List(Of String)
- Dim NumberOfInsertedLines As integer = 0
- For Each (CurrentLine in my csv)
- ' 1. line processing
- Try
- ' (process my line : split, convert, check range...)
- If (I know the insertion will fail) Then
- ' (Store information about that wrong line, in List, log, or do nothing)
- WrongValuedLinesList.Add(" This line : " & CurrentLine
- & " has wrong values because...
- Continue For
- End If
- Catch ex as exception
- ' (here handle the line conversion failed : store in list, or log, or do nothing ...)
- ' for expl :
- ConversionFailedList.Add(" Conversion failed for line " & CurrentLine
- & " exception details : " & ex.message " )
- End Try
- ' 2. Line insertion
- Try
- '(insert my processed data into database)
- NumberOfInsertedLines +=1
- Catch ex as exception
- ' (here handle the insertion failed exception (expl : primary key might not be unique)
- ' : store in list, log, do nothing...)
- ' for example :
- InsertionFailedList.Add(" Insertion failed for line " & CurrentLine
- & " exception details : " & ex.message " )
- End Try
- Next
- (Here you might wanna report how things went to your user using
- your error list.)
- Dim WrongValuedLinesList As New List(Of String)
- Dim ConversionFailedList As New List(Of String)
- Dim InsertionFailedList As New List(Of String)
- Dim NumberOfInsertedLines As integer = 0
- For Each (CurrentLine in my csv)
- ' 1. line processing
- Try
- ' (process my line : split, convert, check range...)
- If (I know the insertion will fail) Then
- ' (Store information about that wrong line, in List, log, or do nothing)
- WrongValuedLinesList.Add(" This line : " & CurrentLine
- & " has wrong values because...
- Continue For
- End If
- Catch ex as exception
- ' (here handle the line conversion failed : store in list, or log, or do nothing ...)
- ' for expl :
- ConversionFailedList.Add(" Conversion failed for line " & CurrentLine
- & " exception details : " & ex.message " )
- End Try
- ' 2. Line insertion
- Try
- '(insert my processed data into database)
- NumberOfInsertedLines +=1
- Catch ex as exception
- ' (here handle the insertion failed exception (expl : primary key might not be unique)
- ' : store in list, log, do nothing...)
- ' for example :
- InsertionFailedList.Add(" Insertion failed for line " & CurrentLine
- & " exception details : " & ex.message " )
- End Try
- Next
- (Here you might wanna report how things went to your user using
- your error list.)
- While dr.Read
- Try
- InsertRowIntoDataBase()
- Catch ex As Exception
- LogErrorToFile(ex)
- End Try
- End While
- While dr.Read
- Try
- InsertRowIntoDataBase()
- Catch ex As SqlClient.SqlException
- LogDataBaseErrorToFile(ex)
- End Try
- End While
- While dr.Read
- Try
- If Not IsRowValid() Then
- LogInvalidDataToFile()
- Continue While
- End If
- InsertRowIntoDataBase()
- Catch ex As SqlClient.SqlException
- LogDataBaseErrorToFile()
- Catch ex As Exception
- LogGenericErrorToFile()
- End Try
- End While
Add Comment
Please, Sign In to add comment