Solved

Do I call Close() AND Dispose() for SqlConnection and SqlDataReader?

Posted on 2010-09-22
4
448 Views
Last Modified: 2012-06-27
I see that SqlConnection and SqlDataReader have both Close and Dispose methods.

Should I call them both?

                reader.Close();
                reader.Dispose();

                conn.Close();
                conn.Dispose();

Thanks,
newbieweb
0
Comment
Question by:newbieweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 167 total points
ID: 33737361
Call
 reader.Close();

to close connection, Why leave it open because you have completed your work with this conneciton. If you have multiple db request call all of them between reader open and close connections. dont leave it open if you dont need it in one shot of page processing. It increased security, durability and performace in your code.


Call
conn.Dispose();

to free up memory for its variable that you declared initiallu.
0
 

Assisted Solution

by:vvr_india
vvr_india earned 167 total points
ID: 33737371
for better programming practice, you do write code like

SqlDataReader DReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
.... do some read statements here

// Summary: (from MSDN)
        //     When the command is executed, the associated Connection object is closed
        //     when the associated DataReader object is closed.

 DReader.Close(); //this statement closes the connection also

0
 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 166 total points
ID: 33737430
Unlike most other objects, you do not need to call Dispose here, if you call Close.

In cases like these, where an ".Open" method exists, it is just more logical to have a ".Close" method.  Dispose does more or less the same thing that Close does.

Calling both of them would not be wrong either, buit I usually do not.

If I remember correctly, a connection object can be reused (connection pooling) if you call Close, but not if you call Dispose, so it is actually better NOT to call Dispose here.
0
 

Author Closing Comment

by:newbieweb
ID: 33737786
Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question