Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Explicitly close DataReader if I close the underlying connection?"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MarkusJNZ@gmail.com - 01 Nov 2006 00:27 GMT
Hi, I have a DataReader which I think may be the cause of connections
remaining open to a MSSQL database.

basically the pseudo code is below

======

SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd = new SqlCommand("Select * from blah",conn);
conn.Open();

SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
// do something
}

conn.Close();
======

Basically, the code closes the connection object but does not close the
acutal dataReader.
Does closing the database connection *also* close the dataReader
connection and remove all references betweent the MSSQL database and
the ADO.NET objects?

Normally, I would do this, so I'm interested in the difference

======

SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd = new SqlCommand("Select * from blah",conn);
conn.Open();

SqlDataReader reader =
cmd.ExecuteReader(CommandBehaviour.CloseConnection);

while(reader.Read())
{
// do something
}

reader.Close();
======
TIA Markus
Carl Daniel [VC++ MVP] - 01 Nov 2006 01:40 GMT
> Hi, I have a DataReader which I think may be the cause of connections
> remaining open to a MSSQL database.
[quoted text clipped - 22 lines]
> connection and remove all references betweent the MSSQL database and
> the ADO.NET objects?

Yes, it should close the connection under the data reader, but it's still
good practice to make sure that your reader is properly cleaned up.

You should use:

SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd = new SqlCommand("Select * from blah",conn);
conn.Open();

using (SqlDataReader reader = cmd.ExecuteReader())
{
   while(reader.Read())
   {
       // do something
   }
}
conn.Close();

...to ensure that the reader is properly disposed.  If your code really is
that linear, then you should use a using block to control the lifetime of
the SqlConnection object as well.  I'm assuming that's just schematic
though, and in reality the code that opens and closes the connection is
farther from the code that uses the reader.

-cd
MarkusJNZ@gmail.com - 01 Nov 2006 06:49 GMT
Thanks for your help
Markus

> > Hi, I have a DataReader which I think may be the cause of connections
> > remaining open to a MSSQL database.
[quoted text clipped - 48 lines]
>
> -cd

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.