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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
Cloudbric
Cloudbric
I
InfoQ
V
V2EX
博客园_首页
The Register - Security
The Register - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
Vercel News
Vercel News
Forbes - Security
Forbes - Security
云风的 BLOG
云风的 BLOG
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
D
DataBreaches.Net
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
A
About on SuperTechFans
N
News and Events Feed by Topic
Apple Machine Learning Research
Apple Machine Learning Research
Help Net Security
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
N
Netflix TechBlog - Medium
Spread Privacy
Spread Privacy
F
Full Disclosure
Recorded Future
Recorded Future
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
The Cloudflare Blog
T
Threatpost
T
Tor Project blog
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
A
Arctic Wolf
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
小众软件
小众软件
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
Security Latest
Security Latest
The Last Watchdog
The Last Watchdog

博客园 - 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.