Search code examples
c#sqlconnection

Do I need to close connection when using a 'using' statement


Currently my code is constructed as follows.

using (var sqlCon = new SqlConnection(Context.ReturnDatabaseConnection()))
        {
            sqlCon.Open();

            try
            {
               //Code here

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                sqlCon.Close();
            }
        }

Ideally from what I understand using the 'using' statement will take care of the connection being closed but I have my doubts due to what other people have said.

Thanks


Solution

  • NO need as object will automatically disposed when we use using blocks. Go through this

    http://msdn.microsoft.com/en-us/library/yh598w02.aspx

    What is the C# Using block and why should I use it?