2011-11-05 59 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