除了記錄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與我嘗試運行的過程一起記錄?
在'using'部分之前拋出'DataException',所以無法使用。 – MatSnow
我已經嘗試了另一種方式,它然後不會拋出新的DataException,如果我把使用事先。 – Dev
你也應該使用['Add'](https://stackoverflow.com/questions/21110001/sqlcommand-parameters-add-vs-addwithvalue)而不是['AddWithValue'](https://stackoverflow.com/questions/21110001/SqlCommand的參數加-VS-addwithvalue)。 – MatSnow