Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 310369 - Last Review: March 9, 2006 - Revision: 1.2
No method in the DataReader object closes its underlying connection
This article was previously published under Q310369
SYMPTOMS
The
DataReader object does not include a property to retrieve its underlying connection. Although you can close
DataReader and then close the
OleDbConnection or
SqlConnection object on which
DataReader was opened,
DataReader does not have a built-in connection.
Thus, if you retrieve a
DataReader from a component method, you cannot close the underlying connection. That is, if you retrieve a
DataReader object from a component, the
DataReader stays connected, and there is no way to disconnect it.
RESOLUTION
To resolve this problem, configure the
ExecuteReader method of a
SqlCommand or
OleDbCommand object so that the
DataReader connection is automatically closed when you close the
DataReader. If you pass the
System.Data.CommandBehavior.CloseConnection flag to the
ExecuteReader method, the
DataReader connection is closed when the
DataReader is closed.
STATUS
This behavior is by design.
For security purposes, a component may not want to expose a connection to unknown code. A malicious user could build an application that uses the connection to compromise data on the server. Thus, the
DataReader does not have a property to expose or manipulate its underlying connection.
This contrasts with the
ADODB.Recordset object, which does include an
ActiveConnection property.
MORE INFORMATION
To pass the
System.Data.CommandBehavior.CloseConnection flag to the
ExecuteReader method, use the following code when you open the
DataReader:
MyDataReader = MyCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
REFERENCES
For more information, refer to the following topics in the Microsoft .NET Software Development Kit (SDK) documentation or the Microsoft Visual Studio .NET Online Help documentation:
APPLIES TO
- Microsoft ADO.NET 2.0
- Microsoft ADO.NET (included with the .NET Framework)
| kbdatabinding kboracle kbprb kbsqlclient kbsystemdata KB310369 |
Retired KB Content DisclaimerThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please