Guest User

Untitled

a guest
Aug 10th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. How to continue insert if one row fails
  2. Dim WrongValuedLinesList As New List(Of String)
  3. Dim ConversionFailedList As New List(Of String)
  4. Dim InsertionFailedList As New List(Of String)
  5. Dim NumberOfInsertedLines As integer = 0
  6.  
  7. For Each (CurrentLine in my csv)
  8. ' 1. line processing
  9. Try
  10. ' (process my line : split, convert, check range...)
  11. If (I know the insertion will fail) Then
  12. ' (Store information about that wrong line, in List, log, or do nothing)
  13. WrongValuedLinesList.Add(" This line : " & CurrentLine
  14. & " has wrong values because...
  15. Continue For
  16. End If
  17. Catch ex as exception
  18. ' (here handle the line conversion failed : store in list, or log, or do nothing ...)
  19. ' for expl :
  20. ConversionFailedList.Add(" Conversion failed for line " & CurrentLine
  21. & " exception details : " & ex.message " )
  22. End Try
  23. ' 2. Line insertion
  24. Try
  25. '(insert my processed data into database)
  26. NumberOfInsertedLines +=1
  27. Catch ex as exception
  28. ' (here handle the insertion failed exception (expl : primary key might not be unique)
  29. ' : store in list, log, do nothing...)
  30. ' for example :
  31. InsertionFailedList.Add(" Insertion failed for line " & CurrentLine
  32. & " exception details : " & ex.message " )
  33. End Try
  34. Next
  35.  
  36. (Here you might wanna report how things went to your user using
  37. your error list.)
  38.  
  39. Dim WrongValuedLinesList As New List(Of String)
  40. Dim ConversionFailedList As New List(Of String)
  41. Dim InsertionFailedList As New List(Of String)
  42. Dim NumberOfInsertedLines As integer = 0
  43.  
  44. For Each (CurrentLine in my csv)
  45. ' 1. line processing
  46. Try
  47. ' (process my line : split, convert, check range...)
  48. If (I know the insertion will fail) Then
  49. ' (Store information about that wrong line, in List, log, or do nothing)
  50. WrongValuedLinesList.Add(" This line : " & CurrentLine
  51. & " has wrong values because...
  52. Continue For
  53. End If
  54. Catch ex as exception
  55. ' (here handle the line conversion failed : store in list, or log, or do nothing ...)
  56. ' for expl :
  57. ConversionFailedList.Add(" Conversion failed for line " & CurrentLine
  58. & " exception details : " & ex.message " )
  59. End Try
  60. ' 2. Line insertion
  61. Try
  62. '(insert my processed data into database)
  63. NumberOfInsertedLines +=1
  64. Catch ex as exception
  65. ' (here handle the insertion failed exception (expl : primary key might not be unique)
  66. ' : store in list, log, do nothing...)
  67. ' for example :
  68. InsertionFailedList.Add(" Insertion failed for line " & CurrentLine
  69. & " exception details : " & ex.message " )
  70. End Try
  71. Next
  72.  
  73. (Here you might wanna report how things went to your user using
  74. your error list.)
  75.  
  76. While dr.Read
  77. Try
  78. InsertRowIntoDataBase()
  79. Catch ex As Exception
  80. LogErrorToFile(ex)
  81. End Try
  82. End While
  83.  
  84. While dr.Read
  85. Try
  86. InsertRowIntoDataBase()
  87. Catch ex As SqlClient.SqlException
  88. LogDataBaseErrorToFile(ex)
  89. End Try
  90. End While
  91.  
  92. While dr.Read
  93. Try
  94. If Not IsRowValid() Then
  95. LogInvalidDataToFile()
  96. Continue While
  97. End If
  98. InsertRowIntoDataBase()
  99. Catch ex As SqlClient.SqlException
  100. LogDataBaseErrorToFile()
  101. Catch ex As Exception
  102. LogGenericErrorToFile()
  103. End Try
  104. End While
Add Comment
Please, Sign In to add comment