Solved

Details about specific commands to close SQLConnection class

Posted on 2013-01-10
6
274 Views
Last Modified: 2013-01-10
Dear experts,

I need a little help for a question connected with Net3.5 SQLConnection class. When I finish usages of SQLDataReader and SQLConnection I know that I should release the recourses, my question is for detail command I should use.

I have 2 SQLDataReaders and 1 SQLConnection – now I use a code like this
readerCamp.Dispose(); reader.Dispose(); conn.Dispose();

The right questions I have are:
1.      Do I really need to close SQLDataReaders, or just conn.Dispose is enogh because it will close on himself all relative connection /SQLDataReaders which used it/
2.      The command conn.Dispose(); is equal to conn=null; - true or not?
3.      Long time I use code like:
if (conn.State != ConnectionState.Closed) conn.Close();
conn.Dispose();

is that necessary? If the result is same  prefer use just conn.Dispose();
0
Comment
Question by:dvplayltd
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38762320
closing the connection will NOT close any data reader or dataset.

apart from that, you may want to use the USING syntax:
using ( SqlConnection cnn = new SqlConnection("connection string") )
{
   cnn.Open();
   using ( SqlCommand cmd = new SqlCommand("select ... ", cnn))
   {
      using (SqlDataReader dr = cmd.ExecuteReader())
      {

      }
   }
   cnn.Close();
}

Open in new window


the "using" will run a dispose automatically at it's end.
of course, you shall put some try{} catch {} handlers inside the different brackets, but that is another part of the programming
0
 

Author Comment

by:dvplayltd
ID: 38762477
I use using on all places where this is practical - but in most place it isn't.

Till now you answer only to my first question - i should close the reader object too, not just its connection. What about other two questions ?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38762482
2.      The command conn.Dispose(); is equal to conn=null; - true or not?
=> it's not "equal" in all cases. if there are other variables pointing to the same object, Dispose will dispose the object; while conn = null would just remove this (local) reference to the object.
if conn is the only (respectively last) reference to the object, it will result indeed in the same effect.

3.      Long time I use code like:
if (conn.State != ConnectionState.Closed) conn.Close();
conn.Dispose();

I prefer to explicitly close the connection to make sure the "server" side of the connection get's a proper disconnection. with dispose this might not be the case.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:dvplayltd
ID: 38762504
10x. I'm starting programming before 13 years with VB6 and C, so I understand the difference between conn=null and conn.Dispose() as you kindly explain me.

So I'll use  conn.Close(); and then conn.Dispose(), only will not chek this if (conn.State != ConnectionState.Closed)  , i think even the connection is not Open Close() will not produce error, right?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38762539
>ConnectionState.Closed)  , i think even the connection is not Open Close() will not produce error, right?

if connection is closed, connection.Close() will raise an error.
0
 

Author Comment

by:dvplayltd
ID: 38762725
Thanks for your last note, i fix my code with global Search and Replace. It show - 55 copies replaced ...

 I say it is not practical to use Using clause as I should have 2-3 levels of nasted code in 4-6 screen pages ... it will make worst reading of the code.
bye and have a nice day!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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