2011-12-14 17 views
0

私はcsvからレコードをフェッチしてsqlテーブルに挿入するwhileループを持っています。現在、csvには多くの行が含まれています。1行が失敗した場合に挿入する方法

私が望むのは、1つの行に失敗しただけでファイルにログを記録し、次のレコードに進む場合です。私は試してキャッチすることを考えていたが、それはプログラムを終了します。だから、どんな提案?

 
while (csv.readnextline) 
'assign csv columns to objects 
try 
'insert to db 
Catch ex As Exception 
'write to log file 
End Try 

例外をキャッチしても上記のコードが必要です。

おかげ

+0

なぜ* catching *例外があなたのプログラムを終了するのですか?いくつかのコードを提供していただけますか? – Markus

+0

okここに例があります:while(file.readnextrecord)db catchの例外ログをファイルに書き込もう...今度は例外が見つかったキャッチでループを再開したい...それがクリアであることを希望します – James

+1

Pleaseサンプルコードで質問を更新してください。 – SWeko

答えて

0

いいえ例外を処理する方法と場所によって、プログラムを終了しません。

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.) 
3

しようとすると、プログラムを終了していないキャッチ、彼らはただ、例外が発生した場合、何かのコードの流れを制御します。

tryブロックで例外が発生すると、(対応する)catchブロックの最初の行で実行が継続されます。 catchブロックの実行後、コードはcatchの後の最初の行に続きます。この場合、ループを続行するEnd Whileになることがあります。
したがって、この

While dr.Read 
    Try 
    InsertRowIntoDataBase() 
    Catch ex As Exception 
    LogErrorToFile(ex) 
    End Try 
End While 

などの建設があなたのために働く必要があります。
しかし、これは、問題が何であっても、データが無効であるか、SQLサーバーが停止しているか、コードにエラーがあっても、例外を生成してログに記録するため、悪い設計ですいくつかのNullReferenceExceptionが潜んでいます)。例外の処理は特定のケースに限定する必要があります。このようなデータベースとの問題、に:また

While dr.Read 
    Try 
    InsertRowIntoDataBase() 
    Catch ex As SqlClient.SqlException 
    LogDataBaseErrorToFile(ex) 
    End Try 
End While 

、データとの可能性のある問題が知られている場合(例えば、整数が期待されているCSVの文字列)それだけで使用するよりも、それをチェックすると良いでしょうこれらの行に沿った例外メカニズムです。

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 
関連する問題