【问题标题】:Retrieving underlying exception from SqlDataReader for INSERT statment从 SqlDataReader 检索 INSERT 语句的基础异常
【发布时间】:2011-11-05 17:18:23
【问题描述】:

我正在执行一条 SQL 插入语句,例如

INSERT INTO Table (fk1, value1) OUTPUT inserted.pk VALUES ('fkv1', 'v1');

其中“pk”是一个自动递增的键值,如:

SqlDataReader reader = cmd.ExecuteReader();

外键与父表冲突,reader.HasRows() 反映了这一点,但是如何检索带有错误描述的实际异常对象?如果删除“OUTPUT”语句,它会很好地抛出异常,但在该语句中,它会吞下错误,并返回“HasRows” == false。

我可以使用“结果视图”属性下的调试器看到错误,但是如何在代码中获取此值?

Sql Server 2008r2 .NET 4.0

感谢您的帮助。

编辑:

此调用不会引发异常。它没有成功完成的唯一迹象是“HasRows”是假的。

【问题讨论】:

  • 你试过用 try/catch 块包围这段代码吗?
  • 确保catch(ExceptionType ex),获取异常信息。

标签: c# .net sql-server ado.net


【解决方案1】:
try
{
   SqlDataReader reader = cmd.ExecuteReader();
}
catch(Exception ex)
{
   string errorMessage = String.Format(CultureInfo.CurrentCulture, 
                         "Exception Type: {0}, Message: {1}{2}", 
                         ex.GetType(),
                         ex.Message,
                         ex.InnerException == null ? String.Empty : 
                         String.Format(CultureInfo.CurrentCulture,
                                      " InnerException Type: {0}, Message: {1}",
                                      ex.InnerException.GetType(),
                                      ex.InnerException.Message));

  System.Diagnostics.Debug.WriteLine(errorMessage);
}

【讨论】:

    【解决方案2】:

    我有一段 sql 正在生成一个异常,但是 try catch 没有捕捉到它 - 这太奇怪了!

    这里是代码

    try
    {
        // Run the SQL statement, and then get the returned rows to the DataReader.
    
        SqlDataReader MyDataReader = MyCommand.ExecuteReader();
    
        //note AT THIS POINT there is an exception in MyDataReader
        //if I view MyDataReader in Watch I see this in base->ResultsView->base
        //Conversion failed when converting the varchar value 'lwang' to data type int
        //and errors count is 1 but there is no exception catch!!
    
        int iRow = 0;
        if (MyDataReader.HasRows)
        {
            int iCol = 0;
            while (MyDataReader.Read())
            {
    
                //dt.Load(MyDataReader);
                List<String> strFields = new List<String>();
    
                for (int iField = 0; iField < MyDataReader.FieldCount; iField++)
                {
                    strReturn = strReturn + MyDataReader[iField] + ",";
                    strFields.Add(MyDataReader[iField].ToString());
                }
                DataRows.Add(strFields);
                iRow++;
            }
        }
    }
    catch (Exception ex)
    {
        //no exception is caught in this case!!  This code is never reached!!
        strError = "An error occurred getting the data table: " + MyCommand.CommandText + " " + ex.ToString();
        throw new Exception(strError);
        return (DataRows);
    }
    finally
    {
        Connection.Close();
    }
        return (DataRows);
    }
    

    如果这里有帮助,那就是正在执行的 sql 正在执行的sql是:

    选择 hec_recommendation.RecID,hec_recommendation.UtilityID,hec_recommendation.CatID,hec_recommendation.Condition,hec_recommendation.RecommendationText,hec_recommendation.Active,hec_recommendation.Ordinal,hec_recommendation.StartDate,hec_recommendation.EndDate,hec_recommendation.CreateDate,hec_recommendation.CreatedByID,hec_recommendation.ModifyDate,hec_recommendation。修改ID 来自 hec_recommendation、hec_utility、hec_reccategory 其中 hec_recommendation.utilityid = hec_utility.id 和 hec_recommendation.catid = hec_reccategory.catid 和 hec_reccategory.utilityid = hec_utility.utilityid 和 hec_utility.utilityId = 'lwang'

    【讨论】:

    • 如果您有任何问题,请发帖... question :-)
    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 2011-02-06
    • 2019-04-30
    • 2019-03-17
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多