问题描述
如何处理多个具有多个行的结果集?调用 NextResult()
会中断while循环.
How to handle multiple ResultSets, each with multiple Rows?The call to NextResult()
breaks the while loop.
我的一些SP返回多个ResultSet.我用 NextResult()
处理这些,但是当我这样做并且我的SP仅具有一个ResultSet时,我看到带有Read()的while循环完成时只剩下第一行.
Some of my SPs return multiple ResultSets. I'm handling these with NextResult()
but when I do and my SP only has a single ResultSet, I see the while loop with Read() finishes leaving me with only the first Row.
没有调用 NextResult()
的情况,我得到了第一个ResultSet的所有行,但是当然不处理第二个及后续ResultSet的行吗?
Without the call to NextResult()
I get all the rows for the first ResultSet but of course the second and subsequent ResultSets don't get processed?
using (IDataReader reader = storedProcedure.ExecuteReader(
CommandBehavior.CloseConnection, parameterNames as string[], arguments))
{
while (reader.Read())
{
ArrayList row = new ArrayList();
for (int j = 0; j < reader.FieldCount; ++j)
{
object rowValue = reader.GetValue(j);
row.Add(rowValue);
}
reader.NextResult();//next resultset, breaks out of the while
}
}
推荐答案
您需要创建两个嵌套循环.
You need to create two nested loops.
- 外部循环应遍历结果集,并在末尾具有
NextResult
- 内部循环应遍历结果集中的行,并在开始处具有
Read
.
类似的东西:
using (IDataReader reader = ...) {
// Process all result sets
do {
// Process all elements in the current result set
while (reader.Read()) {
ArrayList row = new ArrayList();
for (int j = 0; j < reader.FieldCount; ++j) {
object rowValue = reader.GetValue(j);
row.Add(rowValue);
}
// TODO: Do something with 'row'
}
} while (reader.NextResult())
}
这篇关于如何处理多个结果集,每个结果集具有多个行?IDataReader.NextResult()以Read()结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!