问题描述
我在想如果没有与 SQLServer 的连接,SQLDataReader 不应该工作.
I was thinking that the SQLDataReader should not work if there is no connection to the SQLServer.
我试验了这种情况.我执行 ExecuteReader 然后停止 SQLServer 服务并尝试遍历 DataReader.我所期望的是一个例外,但它一个接一个地给出了结果.理想情况下,DataReader 应该从连接到 DB 服务器的流中一次读取一行,如果我们断开 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 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!