SqlDataReader.Read 和 SqlDataReader.NextResult 之间的区别

Difference between SqlDataReader.Read and SqlDataReader.NextResult(SqlDataReader.Read 和 SqlDataReader.NextResult 之间的区别)
本文介绍了SqlDataReader.Read 和 SqlDataReader.NextResult 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种方法的主要区别是什么?在 msdn 网站上,它的解释如下,但我不明白.

What is the main difference between these two methods? On the msdn website it is explained like below but I don't understand it.

Read 将 SqlDataReader 前进到下一条记录.(覆盖DbDataReader.Read().)

Read Advances the SqlDataReader to the next record. (Overrides DbDataReader.Read().)

NextResult 将数据读取器前进到下一个结果,当读取批处理 Transact-SQL 语句的结果时.(覆盖 dbDataReader.NextResult().)

NextResult Advances the data reader to the next result, when reading the results of batch Transact-SQL statements. (Overrides dbDataReader.NextResult().)

推荐答案

如果你的 statement/proc 正在返回多个结果集,例如,如果你在单个 Command 中有两个 select 语句 对象,那么你会得到两个结果集.

If your statement/proc is returning multiple result sets, For example, if you have two select statements in single Command object, then you will get back two result sets.

  • NextResult 用于在结果集之间移动.
  • Read 用于在单个结果集的记录中向前移动.
  • NextResult is used to move between result sets.
  • Read is used to move forward in records of a single result set.

考虑以下示例:

如果你有一个主体是这样的 proc:

If you have a proc whose main body is like:

.... Proc start

SELECT Name,Address FROM Table1

SELECT ID,Department FROM Table2

-- Proc End

执行上述过程会产生两个结果集.一个用于 Table1 或第一个 select 语句,另一个用于下一个 select 语句.

Executing the above proc would produce two result sets. One for Table1 or first select statement and other for the next select statement.

默认情况下,第一个结果集可用于Read.如果要移动到第二个结果集,则需要 NextResult.

By default first result set would be available for Read. If you want to move to second result set, you will need NextResult.

请参阅:使用 DataReader 检索数据

来自同一链接的示例代码: 使用 NextResult 检索多个结果集

Example Code from the same link: Retrieving Multiple Result Sets using NextResult

static void RetrieveMultipleResults(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
          "SELECT EmployeeID, LastName FROM dbo.Employees",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        while (reader.HasRows)
        {
            Console.WriteLine("	{0}	{1}", reader.GetName(0),
                reader.GetName(1));

            while (reader.Read())
            {
                Console.WriteLine("	{0}	{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
            reader.NextResult();
        }
    }
}

这篇关于SqlDataReader.Read 和 SqlDataReader.NextResult 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)