Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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