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

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - 155144

GridView和DataFormatString 32.DataReader和output参数的问题 31.动态SQL中使用变量时,可使用存储过程sp_executesql 【求解算法的时间复杂度的具体步骤】 【指数与对数】 30.一个自定义32进制类初稿 【SQL行转列】 【经典SQL语句 】 28.Lc.exe 已退出,代码 -1 27.PowerDesigner中Stereotype的创建 26.UML笔记(UML2.0设计手册) 25.VSS相关 24.GRIDVIEW相关 23.DOTNET中引用相关 22.使用Castle时,如何获取自定义类的单个属性的PropertyAttribute.Column 21-ReSharper UnitRun for .net 20-系统分析与设计(第5版) 19-关于用例的几点知识——摘自《道法自然》 18-概念性系统设计
29.DataReader相关
155144 · 2008-11-06 · via 博客园 - 155144

1.DataReader没有方法关闭其基础连接

症状
DataReader 对象没有检索其基础连接的属性。 虽然您可以关闭 DataReader,然后关闭用于打开 DataReader 的 OleDbConnection 或 SqlConnection 对

象,但是 DataReader 没有内置的连接。

因此,如果从组件方法检索 DataReader,就无法关闭基础连接。 也就是说,如果从组件检索 DataReader 对象,DataReader 将保持连接,没有方法可以

使它断开连接。


解决方案
若要解决这个问题,可对 SqlCommand 或 OleDbCommand 对象的 ExecuteReader 方法进行配置,使DataReader 连接在您关闭 DataReader 时自动关闭。

如果将 System.Data.CommandBehavior.CloseConnection 标志传递给 ExecuteReader 方法,则当 DataReader 关闭时DataReader 连接也将关闭。

所以在使用完DataReader之后,一定要确保DataReader关闭。可以直接使用DataReader.Close()或DataReader.Dispose(),或者在使用DataReader时,放到using范围里,如using(SqlDataReader rdr = *){}.

另外,直接把 DataReader作为数据源,而且设置为CommandBehavior.CloseConnection时,数据绑定后会自动关闭DataReader,从而关闭DataReader的连接。这是因为数据绑定时,使用MoveNext方法读取数据,该方法在结束后调用DataReader.Close()方法。而DataReader.Read()方法没有。

状态
这种现象是设计使然。 出于安全方面的考虑,组件可能不愿向未知代码公开连接。 怀有恶意的用户可能会生成使用该连接的应用程序,来泄露服务器上的

数据。 因此,DataReader 没有用于公开或操纵其基础连接的属性。

这与 ADODB.Recordset 对象不同,该对象有 ActiveConnection 属性。

_____________________________________________________________________________________
COPYRIGHT©2008,HTTP://ZEROBUG.CNBLOGS.COM .ALL RIGHTS RESERVED.