在C#Catch Exception子句中为另一个DB调用SQL过程

在C#Catch Exception子句中为另一个DB调用SQL过程,c#,sql-server,stored-procedures,C#,Sql Server,Stored Procedures,除了记录SqlException之外,我还尝试调用另一个数据库,该数据库存储最初发生错误的内容。但是,我得到了SqlConnection部分的警告“检测到无法访问的代码”,它肯定没有执行我试图运行的过程 catch (SqlException ex) { throw new DataException(ex.Message, ex); //Call Error Management DB Connection and add content to the table

除了记录SqlException之外,我还尝试调用另一个数据库,该数据库存储最初发生错误的内容。但是,我得到了SqlConnection部分的警告“检测到无法访问的代码”,它肯定没有执行我试图运行的过程

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与我尝试运行的过程一起记录?

您需要在
catch
块末尾抛出异常

如果担心失败,还可以使用语句在另一个try/catch中包装

此外,您需要在执行
SqlCommand
之前打开连接。这就是为什么没有抛出
DataException
,而是在代码到达该行之前抛出了另一个未经处理的异常

例如:


您需要在
catch
块的末尾抛出异常

如果担心失败,还可以使用
语句在另一个try/catch中包装

此外,您需要在执行
SqlCommand
之前打开连接。这就是为什么没有抛出
DataException
,而是在代码到达该行之前抛出了另一个未经处理的异常

例如:


你需要把球投到街区的尽头

您的错误处理代码失败了,这就是为什么您认为它应该位于顶部-可能是因为您没有像其他地方一样打开连接

您还需要捕获在尝试将错误记录到数据库中时可能发生的异常,因为在很多情况下,如果发生第一个数据库错误,那么该操作将失败

然后,您会得到一个非常混乱的异常处理程序,因此您可能应该将它移到另一个方法

           // 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
      }
 }

你需要把球投到街区的尽头

您的错误处理代码失败了,这就是为什么您认为它应该位于顶部-可能是因为您没有像其他地方一样打开连接

您还需要捕获在尝试将错误记录到数据库中时可能发生的异常,因为在很多情况下,如果发生第一个数据库错误,那么该操作将失败

然后,您将得到一个非常混乱的异常处理程序,因此您可能应该将其移动到另一个方法

           // 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
      }
 }

您在
使用
部分之前抛出了一个
新数据异常
,因此无法访问使用。我尝试了另一种方法,如果我事先使用了using,它就不会抛出新的DataException。你也应该使用而不是。你在
using
-部分之前抛出一个
新DataException
,因此无法使用。我试过另一种方法,如果我事先设置了using,那么它不会抛出新的DataException。而且您应该使用而不是。谢谢!添加try-catch子句并在事件查看器中显示异常指出了新SQL连接中的错误。谢谢!添加try-catch子句并在事件查看器中显示异常指出了新SQL连接中的错误。