如何DataReader的工作? [英] How DataReader works?

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

问题描述

我在想,SqlDataReader的不应该工作,如果没有对SQLServer的任何连接。

I was thinking that the SQLDataReader should not work if there is no connection to the SQLServer.

我尝试了这种情况。我执行的ExecuteReader然后停止SQLServer服务,并试图遍历DataReader的。我的预期是一个例外,但它给了后,其他的结果之一。理想情况下,DataReader的应读一行从数据流的时刻,被连接到数据库服务器并且如果我们断开DB服务器应该抛出一个异常?

I experimented this scenario. I execute the ExecuteReader then stop the SQLServer Service and tried to iterate through the DataReader. What I expected was an exception, but it gave the results one after the other. Ideally the DataReader should read one row at a time from the stream that gets connected to the DB server and which should throw an exception if we disconnect the DB server?

我不知道,这是什么,我在这里失踪。

I don't know, What is it that I am missing here.

推荐答案

我强烈怀疑,读者一次读入一批结果。这是一个很多比一行更有效的时间(考虑在单行只有几个字节的情况......你不希望每行的网络数据包时,它可以检索到大量的行中的单个数据包)。它也可能允许数据库早期释放其内部资源。 - 如果所述数据读取器读取了所有的结果(当只有几个),它可以有效地忘记有关查询

I strongly suspect that the reader reads a batch of results at a time. That's a lot more efficient than one row at a time (think about the situation where a single row is only a few bytes... you don't want a network packet per row when it could have retrieved lots of rows in a single packet). It also potentially allows the database to release its internal resources early - if the data reader has read all the results (when there are only a few) it can effectively forget about the query.

我想,如果你尝试与返回的大量结果的查询同类型的东西,你会得到预期的异常。

I suspect if you try the same type of thing with a query returning lots of results, you'd get the expected exception.

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

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