2011-02-15 51 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());