2011-11-05 61 views
1

我正在执行一个SQL插入语句,例如从SqlDataReader中为INSERT语句检索潜在的异常

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

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

SqlDataReader reader = cmd.ExecuteReader(); 

外键与父表冲突,而reader.HasRows()反映了这一点,但如何我可以检索出现错误描述的实际异常对象吗?如果你删除了“OUTPUT”语句,它会抛出异常,但是在那里有这个语句,它会吞下错误,并且只返回“HasRows”== false。

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

SQL服务器2008R2 .NET 4.0

感谢您的帮助。

编辑:

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

+5

你是否尝试用try/catch块来围绕这段代码? –

+0

确保catch(ExceptionType ex)',以获取异常的信息。 – Ryan

回答

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

我有一块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.C reatedByID,hec_recommendation.ModifyDate,hec_recommendation.ModifyByID 从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'

+3

如果你有问题,请发表... [问题](http://stackoverflow.com/questions/ask):-) – kleopatra