How DataReader works?(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 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:DataReader 是如何工作的?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01