DataReader 是如何工作的?

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

问题描述

我在想如果没有与 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 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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子句?)