2017-10-11 10 views
0

SqlExceptionのロギングと同時に、最初にエラーが発生した場所のコンテンツを格納している別のDBを呼び出そうとしています。しかし、私はSqlConnectionの部分に「Unreachable code detected」という警告が表示されています。実行しようとしているプロシージャは実行されていません。C#例外例外句で別のDBのSQLプロシージャを呼び出す

catch (SqlException ex) 
{ 
    throw new DataException(ex.Message, ex); 

    //Call Error Management DB Connection and add content to the table 
    using (SqlConnection connection = new SqlConnection(_errorManagementConnectionString)) 
    { 
      SqlCommand cmd = new SqlCommand("[dbo].[InsertDataErrors]", connection); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@InputParam", InputParam); 
      cmd.Parameters.AddWithValue("@Content", Content); 
      cmd.ExecuteNonQuery(); 
      connection.Open(); 
    } 
} 

どのように私は、このエラーに取り組み、SQLExceptionが、私が実行しようとしている手順と一緒にログに記録されていることを確認することができますか?

+1

あなたは 'using'部分の前に' new DataException'を投げていますので、使用できません。 – MatSnow

+0

別の方法で試してみましたが、あらかじめ使用しておくと、新しいDataExceptionがスローされません。 – Dev

+0

また、['AddWithValue'](https://stackoverflow.com/questions)の代わりに[' Add'](https://stackoverflow.com/questions/21110001/sqlcommand-parameters-add-vs-addwithvalue)を使用してください。/21110001/sqlcommand-parameters-add-vs-addwithvalue)。 – MatSnow

答えて

4

catchブロックの最後に例外をスローする必要があります。

もしあなたがその失敗を心配しているなら、usingステートメントを別のtry/catchにラップすることもできます。

また、SqlCommandを実行する前に接続を開く必要があります。このため、DataExceptionがスローされていないため、コードがその行に到達する前に別の未処理の例外がスローされていました。

例えば:あなたはブロックの最後にスローを配置する必要があり

catch (SqlException ex) 
{ 
    try 
    { 
     //Call Error Management DB Connection and add content to the table 
     using (SqlConnection connection = new SqlConnection(_errorManagementConnectionString)) 
     { 
      SqlCommand cmd = new SqlCommand("[dbo].[InsertDataErrors]", connection); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@InputParam", InputParam); 
      cmd.Parameters.AddWithValue("@Content", Content); 
      connection.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
    } 
    catch 
    { 
     // Add any desired handling. 
    } 

    throw new DataException(ex.Message, ex); 
} 
+0

ありがとうございます! try-catch句を追加し、イベントビューアに例外を表示すると、その新しいSQL接続のエラーが指摘されました。 – Dev

0

あなたのエラー処理コードは失敗しています。なぜなら、おそらくあなたは別の場所で接続を開いていないため、それが一番上にあるはずだと思います。

DBにエラーを記録しようとすると発生する可能性のある例外をキャッチする必要があります。なぜなら、最初のデータベースエラーが発生した場合に失敗するケースがかなりあるからです。

次に、本当に面倒な例外ハンドラになるので、別のメソッドに移動する必要があります。

  // somehting with database .... 
     } 
     catch (SqlException ex) 
     { 
      AttemptToLogDbError(ex) 
      throw new DataException(ex.Message, ex); 
     } 

     // ... 

} 

void AttemptToLogDbError(SqlException ex) 
{ 
     try 
     { 
      //Call Error Management DB Connection and add content to the table 
      using (SqlConnection connection = new SqlConnection(_errorManagementConnectionString)) 
      { 
       connection.open(); 
       SqlCommand cmd = new SqlCommand("[dbo].[InsertDataErrors]", connection); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.AddWithValue("@InputParam", InputParam); 
       cmd.Parameters.AddWithValue("@Content", Content); 
       cmd.ExecuteNonQuery(); 
      } 
     } 
     catch (Exception err) 
     { 
      // OMG nothing is working at all! log/report this somehow, but do not throw 
     } 
} 
関連する問題