如何处理多个结果集,每个结果集有多行? IDataReader.NextResult() 结束 Read()

发布于 2024-09-26 01:45:45 字数 761 浏览 0 评论 0原文

如何处理多个结果集,每个结果集有多行? 对 NextResult() 的调用会中断 while 循环。

我的一些 SP 返回多个结果集。我使用 NextResult() 处理这些,但是当我这样做并且我的 SP 只有一个 ResultSet 时,我看到带有 Read() 的 while 循环完成后只剩下第一行。

如果不调用 NextResult() 我会获取第一个 ResultSet 的所有行,但当然第二个和后续 ResultSet 不会得到处理?

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
    }
}

How to handle multiple ResultSets, each with multiple Rows?
The call to NextResult() breaks the while loop.

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.

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-10-03 01:45:45

您需要创建两个嵌套循环。

  • 外部循环应迭代结果集,并应在末尾包含 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())
} 

You need to create two nested loops.

  • The outer loop should iterate over result sets and should have NextResult at the end
  • The inner loop should iterate over rows in a result set and should have Read at the beginning.

Something like:

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())
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文