【问题标题】:stored procedure returns multiple resultsets but number of resultsets are not fixed存储过程返回多个结果集,但结果集的数量不固定
【发布时间】:2011-06-27 07:58:57
【问题描述】:

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

【问题讨论】:

  • 您遇到什么错误?文档说如果没有更多的结果集,NextResult 方法应该简单地返回 false。

标签: c# stored-procedures sqldatareader


【解决方案1】:

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

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

【讨论】:

  • 在 dataReader.NextResult() 上,我得到错误 'dataReader.NextResult()' throw an exception of type 'System.Data.SqlClient.SqlException' bool {System.Data.SqlClient.SqlException} 。只有在没有更多结果集时才会出现此错误。
  • 错误可能在您的查询中。发布一个带有代码清单的新问题,以及完整的异常消息。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多