Solved

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

Posted on 2010-09-22
4
438 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now