-1

This is a follow up question to this question:

Correct use of Try Catch for the SQL connection in C#

when you write a code like:

using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand cmd = new SqlCommand(queryGetPcPrintDetails, connection))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            //do stuff
                        }
                    }
                } // reader closed and disposed up here

            } // command disposed here
        }

Is there a need to catch an exception in order to close the connection? For example, if there's some problem in the second using or in the do stuff section.. do I need to somehow do try/finally and close the connection?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 4
    If it's in a using, it'll get disposed: ["Clean up resources allocated with either using statements, or finally blocks. Prefer using statements to automatically clean up resources when exceptions are thrown. Use finally blocks to clean up resources that don't implement IDisposable."](https://learn.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions#use-trycatchfinally-blocks-to-recover-from-errors-or-release-resources). – 15ee8f99-57ff-4f92-890c-b56153 May 20 '19 at 13:44
  • a `using` is just a shorthand for `try{..}finally{obj.Dispose()}` – Liam May 20 '19 at 13:49
  • 1
    https://stackoverflow.com/questions/4717789/in-a-using-block-is-a-sqlconnection-closed-on-return-or-exception so many questions that address this here on SO... – Trevor May 20 '19 at 13:49

1 Answers1

0

Whenever you create connection inside using{} block, connection get automatically close at the end of using block/scope.Automatically in the sense, Dispose() method get called whenever you leave the scope of Using{}

SQL connection class : MSDN

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • even in case there's an exception before you reach the end of the using scope? – CodeMonkey May 20 '19 at 13:48
  • @YonatanNir Dispose() gets called *when you leave* the scope at any point, by whatever means. A return statement on the first line of the scope would cause Dispose() to get called. "At the end" isn't quite how I'd express the concept. – 15ee8f99-57ff-4f92-890c-b56153 May 20 '19 at 13:49
  • @EdPlunkett, already answered your question.. I want to know why people downvoted – Prasad Telkikar May 20 '19 at 13:50
  • I am seeing 0 for votes, what's the issue? Just my two cents, it's been addressed many times before, we don't need another... I could have answered and so could of others, but they may have felt that a comment would better suffice, similar/exact questions already on SO etc... – Trevor May 20 '19 at 13:52
  • 1
    @PrasadTelkikar Not my question. I did not downvote. I suspect they may have downvoted because it's a very terse answer with no references, and/or because "at the end" is a very easily misunderstood way to describe what's happening. You'll rarely know why people downvote around here; perhaps they assume it should be as obvious to you as it is to them (though if it were, you'd hardly have written what you did). – 15ee8f99-57ff-4f92-890c-b56153 May 20 '19 at 13:52
  • @EdPlunkett, totally agree with your point... I updated my answer with msdn link – Prasad Telkikar May 20 '19 at 14:01
  • @PrasadTelkikar Very often, when you give a reason, they argue and it's a waste of everybody's time. – 15ee8f99-57ff-4f92-890c-b56153 May 20 '19 at 14:03
  • People downvoted because they don't like when people are answering questions they think that should be closed. So you got downvoted for helping – CodeMonkey May 20 '19 at 16:00