2017-10-11 30 views
0

除了記錄SqlException之外,我還試圖調用另一個存儲最初發生錯誤的內容的數據庫。但是,我得到SqlConnection部分的警告'Unreachable code detected',它肯定沒有執行我試圖運行的過程。在C#Catch異常子句中調用另一個數據庫的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'部分之前拋出'DataException',所以無法使用。 – MatSnow

+0

我已經嘗試了另一種方式,它然後不會拋出新的DataException,如果我把使用事先。 – Dev

+0

你也應該使用['Add'](https://stackoverflow.com/questions/21110001/sqlcommand-parameters-add-vs-addwithvalue)而不是['AddWithValue'](https://stackoverflow.com/questions/21110001/SqlCommand的參數加-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

你需要把扔在塊的結尾。

您的錯誤處理代碼失敗,這就是爲什麼您認爲它應該在頂部 - 可能是因爲您沒有像其他地方打開連接。

您還需要捕獲當您嘗試在數據庫中記錄錯誤時發生的可能的異常,因爲如果發生第一個數據庫錯誤時會出現失敗的情況。

然後你最終得到一個非常混亂的異常處理程序,所以你可能應該將它移動到另一個方法。

  // 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 
     } 
} 
相關問題