Difference between SqlDataReader.Read and 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用于在单个结果集的记录中向前移动.
NextResultis used to move between result sets.Readis 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 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SqlDataReader.Read 和 SqlDataReader.NextResult 之间的区别
基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
