Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:07:14+00:00 2026-05-16T12:07:14+00:00

In code, say we have: using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[LocalSqlServer].ToString())) { cn.Open(); //

  • 0

In code, say we have:

using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()))
{
    cn.Open();

    // Set all previous settings to inactive
    using (SqlCommand cmd = new SqlCommand("UPDATE tblSiteSettings SET isActive = 0", cn))
    {                            
        cmd.ExecuteNonQuery();
    }

    cn.Close();
}

The cn.close is not technically required as garbage collection will take care of the connection for us.

However, I always like to close it anyway and to not rely on garbage collection. Is this bad? A waste of time? Or considered good practise to not rely on automation?

Thanks for your thoughts and opinions in advance. I’ll mark this as community wiki as it’s probably subjective.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:07:15+00:00Added an answer on May 16, 2026 at 12:07 pm

    You should never rely on the GC for this. Raymond Chen’s blog article about this is a good starting point. Essentially, if you don’t manually Close/Dispose of your connection, then there is no guarantee that it will happen, because otherwise it would only ever happen when Dispose was called from the Finalizer which might not ever happen:

    A correctly-written program cannot
    assume that finalizers will ever run
    at any point prior to program
    termination.

    Yes, in practice, the finalizer for your connection will probably eventually happen, but even then, you are holding onto a live connection for longer than you actually need to. This can create issues if the database only allows a limited number of live connections at any one time.

    What you’re doing is considered good practice: when you’re done with resources, free them up. If an object is IDisposable, Dispose of it when you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Edit: I've added in the months_until_next_payment column. This would be… May 17, 2026 at 1:13 am
  • Editorial Team
    Editorial Team added an answer Try Columns[groupBox4.Tag.ToString()] for the first error. Regarding tags, in C#… May 17, 2026 at 1:13 am
  • Editorial Team
    Editorial Team added an answer Have you tried replacing http://www.sitemaps.org/schemas/sitemap-image/1.1 with http://www.google.com/schemas/sitemap-image/1.1 since that seem… May 17, 2026 at 1:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Say I have a textbox in HTML using the following code: <input type=text name=text
Say you have some boiler plate using statements. Say something like this: #if !NUNIT
Say I have a class Code defined like this, with a user specified type
Lets say I have two swfs A and B, and at runtime, swf A
I have been using the jQuery nyroModal plugin for a little bit of time,
Let's say I have written a Class Library (dll) in .Net and now I
Say i have a several list if ints: x = [['48', '5', '0'], ['77',
I have been using ActiveResource in my Ruby on Rails applications for some time,
We are using TFS Build 2010 for Builds. We have two branches of source
Let's say I have a class class SomeClass { public void methodA() {} public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.