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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Agent

更改VS.NET 默认SCM Provider的方法 关于VS.NET的SCM Plug-in Lippman的宣传 上海照的 - Agent 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.