惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
Scott Helme
Scott Helme
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
J
Java Code Geeks
U
Unit 42
The GitHub Blog
The GitHub Blog
H
Help Net Security
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
T
Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
L
LINUX DO - 最新话题
K
Kaspersky official blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Check Point Blog
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
Y
Y Combinator Blog
N
News | PayPal Newsroom
M
MIT News - Artificial intelligence
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
I
Intezer
爱范儿
爱范儿
F
Fortinet All Blogs
P
Palo Alto Networks Blog
C
CERT Recently Published Vulnerability Notes

博客园 - Agent

更改VS.NET 默认SCM Provider的方法 关于VS.NET的SCM Plug-in Lippman的宣传 上海照的 cookie的过期时间-- 郁闷又恼火 - Agent - 博客园 可为空的基础类型 Bad Smell Choice 做了一个小型的Dump以便于调试使用 - Agent - 博客园 It's Great VS2005 VS2005 My Assembler 有关JIT 凌晨三点不想睡 不学编程 别搞计算机了 天又要亮了 该死的宏 Uncle bob's View of dynamic and static languages
Important Issue About DataReader and DataGrid's Paging
Agent · 2004-10-16 · via 博客园 - Agent

使用DataReader的重要提示:
在Reader没有读完数据之前, Reader是不会自动关闭的。

Portal的DAL中大量使用了SqlDataReader用在DataBinding中以提高效率, 不知道有没有注意到在使用中都显式的关闭了DataReader. 若数据在DataBinding的过程中全部读出,可以不需要手动去关闭DataReader。但是如果数据在读出过程中中断(这种情况经常出现),就一定要手动关闭DataReader。

比如说,由于DataGrid的绑定DataTable进行默认分页太慢,数据量太大,消耗内存太多而使用DataReader进行CustomPaging的时候,绝对不能如同DataTable一般的直接取出全部数据绑定,也就是说,不能取出数量大于PageCount的数据。因为DataGrid只绑定PageCount个内容, 而你的DataReader要读的内容大于需要的内容,所以DataReader在绑定结束后,DataReader.Read()依然返回true, 这样DataReader是不会自动关闭自己及连接。这样会造成严重的资源泄露。

因此,如果想偷懒。就一定要算好数据量再读取,不要忽视Reader没有读完的情况。如果想要稳定,就一定要记得在任何时候, 用完了DataReader一定要关闭(如果没有CommandBehavior.CloseConnection, 还要关闭连接)。 使用DataReader进行数据绑定在不分页能完全显示的情况下是非常优越的,不需要处理细节问题。但是在数据需要分页的时候,不管怎么样,请你记得关闭DataReader, 或者是计算好你的数据。

再贴一个来自MS ADO.NET (Core Ref)中的QA

  • Q. I called a stored procedure that returns a set of rows. Everything seems to work except that the output and return parameters are empty. Why is that?

  • A. You can think of a stored procedure as a function in your code. The function doesn’t return a value until it has executed all of its code. If the stored procedure returns results and you haven’t finished processing these results, the stored procedure hasn’t really finished executing. Until you’ve closed the DataReader, the return and output parameters of your Command won’t contain the values returned by your stored procedure.

    Let’s say we have the stored procedure

    CREATE PROCEDURE RowsAndOutput (@OutputParam int OUTPUT) AS
        SELECT @OutputParam = COUNT(*) FROM Customers
        SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers

    and we call it with the following code:

  • 
    

    Even though the stored procedure sets the value of the output parameter before running the query that returns rows from the Customers table, the value of the output parameter is not available until after the DataReader is closed.