?
Solved

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

Posted on 2010-09-22
4
Medium Priority
?
458 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 668 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 668 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 664 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 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