0

We have error handling that logs exceptions into our database. An error is logging that is just a SQL Server column name, nothing more. The errors we log are the exception message concatenated with the inner exception. So we'd expect to see the kind of exception and more detail, but instead all we see is ProblemField

We are writing in C# on the .Net 2.0 Framework and using SQL Server 2005 for the DB.

Here's the code, along with issues I noticed.

using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection))
{
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@PARAM1", var1);
   cmd.Parameters.AddWithValue("@PARAM2", var2);
   cmd.Parameters.AddWithValue("@PARAM3", var3);

   using (SqlDataReader rdr = cmd.ExecuteReader())
   {
       if (rdr.HasRows)
       {
           rdr.Read();

           var4 = (double)rdr["ProblemField"];
           var5 = (double)rdr["OtherField"];
       }
       else
       {
           var4 = var5 = 0.0;
       }
   }
}

I changed these reads of rdr["ProblemField"] and rdr["OtherField"] to use a Double.TryParse instead.

Select of MyStoredProcedure:

SELECT TOP 1 [anbrID]
        ,[col1]
        ,[col2]
        ,[col3]
        ,COALESCE([ProblemField], 0.0)
        ,COALESCE([OtherField], 0.0)
        ,[col6]
        FROM MyTable
        WHERE (col1=@PARAM1 OR
            CAST(@PARAM2 AS int) between CAST(col2 AS int) and CAST(col3 AS int)) AND
            col6 =@PARAM3
        ORDER BY id DESC

I have changed these COALESCEs to also have an as "ProblemField," etc..

Bottom line: Is this kind of exception message normal? Do my fixes seem reasonable for such a vague error?

4

1 回答 1

2

这就是为什么(除其他原因外)您应该记录整个异常(使用 .ToString)以便获得堆栈跟踪和异常类型。

不过,我会冒险猜测,这是 SqlDataReader 抛出的 ArgumentOutOfRangeException - 我相信它只给出了 field 参数而没有任何其他消息。您目前没有合并列的“as”,这肯定会导致这种情况。你说你添加了它,但我怀疑它不正确。

于 2013-07-08T16:10:29.473 回答