2011-02-15 50 views
1

我有一個存儲過程返回多個結果集的變量數。如果沒有下一個結果集存在,DataReader.NextResult()會給出錯誤。如何查找下一個結果集是否存在。如果有更多的結果集存儲過程返回多個結果集,但結果集的數量不固定

+1

你有什麼錯誤?文檔說,如果沒有更多的結果集,NextResult方法應該簡單地返回false。 – 2011-02-15 02:46:35

回答

4

的NextResult()方法返回true - 製作之前檢查你的下一個閱讀

​​

+0

在dataReader.NextResult()上,我得到錯誤'dataReader.NextResult()'拋出了一個'System.Data.SqlClient.SqlException'類型的異常bool {System.Data.SqlClient.SqlException}。只有當沒有更多結果集時纔會出現此錯誤。 – FrankSmith 2011-02-15 17:18:40

0

(我知道這是舊的文章,但希望這是有幫助的有人)

如果你需要處理未知數量的結果集,你可以這樣做以下:

// Need to wrap the while loop in a do-while loop due to the way Read() works versus NextResult(). 
// Read() moves to the next record, if any, starting with the first record. 
// NextResult() moves to the next result set, if any, starting with the second result set (i.e., first result set is used automatically). 
do 
{ 
    while (mySqlDataReader.Read()) 
    { 
     // Do some processing here...for example: 

     var rowValues = new object[mySqlDataReader.FieldCount]; 
     mySqlDataReader.GetValues(rowValues); 

     foreach (var element in rowValues) 
     { 
      myStringBuilder.Append(element).Append(" | "); 
     } 

     myStringBuilder.AppendLine(); 
    } 
} 
while (mySqlDataReader.NextResult());