从SqlDataReader中为INSERT语句检索底层异常

本文关键字:异常 检索 语句 SqlDataReader 中为 INSERT | 更新日期: 2023-09-27 18:13:47

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

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

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

SqlDataReader reader = cmd.ExecuteReader();

外键与父表冲突,reader.HasRows()反映了这一点,但是我如何检索与错误描述一起抛出的实际异常对象?如果您删除"OUTPUT"语句,它会抛出异常,但有了该语句,它就会吞下错误,只返回"HasRows"== false。

我可以看到在"结果视图"属性下使用调试器的错误,但我怎么能在代码中得到这个值?

Sql Server 2008r2net 4.0

谢谢你的帮助。

编辑:

这个调用不会抛出异常。它没有成功完成的唯一迹象是"HasRows"为假。

从SqlDataReader中为INSERT语句检索底层异常

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);
}

我有一段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。条件,hec_recommendation。RecommendationText hec_recommendation。活跃,hec_recommendation。序数,hec_recommendation。StartDate可以,hec_recommendation。EndDate hec_recommendation。CreateDate hec_recommendation。CreatedByID hec_recommendation。ModifyDate hec_recommendation。ModifyByIDFrom hec_recommendation, hec_utility, hec_reccategory wherehec_recommendation。Utilityid = hec_utility。id和hec_recommendation。Catid = hec_reccategory。catid和hec_reccategory。Utilityid = hec_utility。utilityid和hec_utility。utilityId = 'lwang'