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

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

Should I call them both?



Question by:newbieweb
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

Accepted Solution

Mohit Vijay earned 167 total points
ID: 33737361

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.


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

Assisted Solution

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

LVL 10

Assisted Solution

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.

Author Closing Comment

ID: 33737786

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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