Is CommandBehavior.CloseConnection enough to close connection if exception occurs?

I have a method that opens a DataReader. When obtaining the DataReader via the SqlCommand.ExecuteReader method I use the command behavior "CommandBehavior.CloseConnection" to make sure the connection is closed when the reader is closed.

  But what happens if an exception iccurs during the "SqlCommand.ExecuteReader" statement? What happens to the Sql Connection? Do I need to place a SqlConnection.Close() explicitly in the "try .. catch .. finally" so that SqlConnection is closed properly? (Example below)
SqlDataReader sdr = null;
 
try
{
   string sql = <MY SQL STATEMENT>;
   sqlConn = new SqlConnection(this.SqlConnectionString);
   SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
   sqlCmd.CommandType = CommandType.Text;
   sqlConn.Open();
   sdr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
   return sdr;
}
catch (Exception ex)
{
   <HANDLE EXCEPTION HERE>
}
finally
{
   if (sqlConn != null && sqlConn.State == ConnectionState.Open)
   {
      sqlConn.Close();
   }
}

Open in new window

thecodingfleaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mohan_sekarCommented:
Yes, close and free resources in the finally block
0
thecodingfleaAuthor Commented:
So I am assuming then that it is not enough to rely on CommandBehavior.CloseConnection to close the database connection? Thanks.
0
jjardineCommented:
use the using block for this....

using (SqlConnection sqlConn = new SqlConnection(this.SqlConnectionString))
{
    All your cmd and datareader stuff in here.   You could use the using block with the Command object as well.
}

The using block automatically calls the dispose method of your object.  So if your reader threw an exception, before leaving the using block, the connection's dispose would get called, which by default closes the connection.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GuitarRichCommented:
the using object will only be useful if any databinding or using of the data reader is inside that using block. You are correct in using the CommandBehaviour.CloseConnection - this will allow you to return the data to the code behind in the UI, bind it and then clean it up there. If you try to return the SqlDataReader while inside the using block you will get an exception as soon as you use it as the connection will no longer be open.
Its not enough to rely on that to close the connection in the event of an exception, because the error more than likely is going to stop the data reader being initialised and loaded properly. So you will need to do it in the finally block as you have done.
0
Anurag ThakurTechnical ManagerCommented:
i will be using what jjardine is suggesting becaue even when an exception will occur and the control jumps out of the using block the connection will be disposed automatically
read the following article and it explains in detail how will the connection object disposes
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.