SqlDataReader.Read()如何在内部工作? [英] How does SqlDataReader.Read() work internally?

查看:187
本文介绍了SqlDataReader.Read()如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在经典的ADO .NET中,有一个非常简单的命令用于读取表中的行.它称为SqlDataReader.Read():

//初始化逻辑
...
使用(SqlDataReader reader = cmd.ExecuteReader())

而(reader.Read())
{
    //行提取逻辑
} 

我了解到reader.Read()返回一个布尔值,并且基于其值,光标将继续读取下一条记录.

我不理解的精确地是它实际上如何将SqlDataReader推进到下一条记录,如文档所示.

我检查了.Read()的内部代码,只是发现了一个非常复杂的逻辑集,该逻辑集具有我无法立即理解的流程-尤其是子方法TryReadInternal().

内部逻辑中发生了很多事情,我只是想清楚地了解它实际上是如何允许SqlDataReader从当前记录中读取数据并移至下一条记录的.毕竟,它只是返回一个布尔值(或者是?), 还是为SqlDataReader做更多的工作,而不只是返回一个布尔值?它是否用一行填充SqlDataReader,然后返回布尔值?这到底是怎么回事? .Read()如何允许SqlDataReader获取行数据和光标位置?

我之所以这样问,是因为似乎没有.Read()命令,我无法从表中读取数据,因此它必须在幕后做一些特殊的事情.

谢谢.


解决方案



In classic ADO .NET, there is a very simple command for reading rows from a table. It is called SqlDataReader.Read():

// Initialization logic
...
using (SqlDataReader reader = cmd.ExecuteReader())

while (reader.Read())
{
    // Row fetching logic
}

I understand that reader.Read() returns a Boolean and based on its value, the cursor will continue reading the next record. 

What I don't understand precisely is how it actually advances the SqlDataReader to the next record, as the documentation indicates.

I have inspected the internal code of .Read(), only to find a very complicated set of logic which has a flow that I don't immediately understand - especially the submethod TryReadInternal(). 

A lot of things are going on in this internal logic and I just want to get a clear idea of how it essentially allows the SqlDataReader to read data from the current record and move on to the next record. After all, it's just returning a Boolean (or is it?), or is it doing more for the SqlDataReader than just returning a Boolean? Does it populate the SqlDataReader with a row and then return a boolean? What exactly is going on here? How is .Read() allowing the SqlDataReader to get row data and a cursor position?

I'm asking this because it seems that without the .Read() command, I can't read data from the table, so it must be doing something special behind the scenes.

Thanks.


解决方案

https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetdataproviders

Go ask the horse....


这篇关于SqlDataReader.Read()如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆