【发布时间】:2011-06-27 07:58:57
【问题描述】:
我有一个存储过程,它返回可变数量的多个结果集。如果不存在下一个结果集,DataReader.NextResult() 会给出错误。如何查找下一个结果集是否存在。
【问题讨论】:
-
您遇到什么错误?文档说如果没有更多的结果集,NextResult 方法应该简单地返回 false。
标签: c# stored-procedures sqldatareader
我有一个存储过程,它返回可变数量的多个结果集。如果不存在下一个结果集,DataReader.NextResult() 会给出错误。如何查找下一个结果集是否存在。
【问题讨论】:
标签: c# stored-procedures sqldatareader
如果有更多结果集,则 NextResult() 方法返回 true - 在进行下一次读取之前检查一下
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult.aspx
【讨论】:
(我知道这是一篇旧帖子,但希望这对某人有所帮助!)
如果您需要处理未知数量的结果集,您可以执行以下操作:
// 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());
【讨论】: