存储过程返回多个结果集,但结果集的数量不固定

发布于 2024-10-17 04:44:50 字数 87 浏览 6 评论 0原文

我有一个存储过程,它返回可变数量的多个结果集。如果不存在下一个结果集,DataReader.NextResult() 会给出错误。如何查找下一个结果集是否存在。

I have a stored procedure which returns variable number of multiple resultsets. DataReader.NextResult() gives error if no next resultset exists . How to find whether next resultsets exists or not.

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

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

发布评论

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

评论(2

回忆那么伤 2024-10-24 04:44:50

如果有更多结果集,NextResult() 方法将返回 true - 在进行下一次读取之前检查这一点

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult.aspx

The NextResult() method returns true if there are more result sets - check that before making your next read

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult.aspx

只是在用心讲痛 2024-10-24 04:44:50

(我知道这是一篇旧文章,但希望这对某人有帮助!)

如果您需要处理未知数量的结果集,您可以执行以下操作:

// 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());

(I know this is an old post, but hopefully this is helpful to someone!)

You can do something like the following if you need to process an unknown number of result sets:

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