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

  • SEARCH
  • Home
  • 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 1038103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:55:59+00:00 2026-05-16T14:55:59+00:00

I use the using statement for SqlConnection . It’s is good for performance because

  • 0

I use the using statement for SqlConnection. It’s is good for performance because forces calling Dispose() that simply releases the connection to the pool sooner.

However, I realized that object created in using cannot be redefined. I cannot do like this:

   using (SqlConnection connection = new SqlConnection(connectionString))
   {
       connection.Open();
       //...
       connection = new SqlConnection(connectionString2);
       //...
       connection = new SqlConnection(connectionString3);
   }

I was wondering if I can replace using, and do something like this:

 {
       SqlConnection connection = new SqlConnection(connectionString);

       connection.Open();
       //...
       connection = new SqlConnection(connectionString2);
       //...
       connection = new SqlConnection(connectionString3);
 }

The SqlConnection will not be accesible after last } brace. Will be the Dispose() called immediatly when object goes out of scope?

  • 1 1 Answer
  • 1 View
  • 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-16T14:56:00+00:00Added an answer on May 16, 2026 at 2:56 pm

    No, things won’t get automatically cleaned up in your second example (in fact, with the code you have, you’ll leave several connections hanging open).

    Not only that, but you lose the automatic cleanup in case of Exceptions being thrown inside the using block. Remember that a using block decomposes into:

    SqlConnection connection = new SqlConnection(connectionString);
    try
    {
        connection.Open();
        // Do work
    }
    finally
    {
        connection.Dispose();
    }
    

    If you’re really using different connections and each connection is Disposed of at the end of the block, I would use several using blocks:

    using(SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        // Do Work
    }
    
    // First connection is disposed
    
    using(SqlConnection connection = new SqlConnection(connectionString2))
    {
        // Do More Work
    }
    
    // Second connection is disposed
    
    using(SqlConnection connection = new SqlConnection(connectionString3))
    {
        // Do More Work
    }
    
    // Last connection is dipsosed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is the <map> HTML element that allows the definition… May 16, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer Supply your own custom ListAdapter to the AlertDialog.Builder via setAdapter().… May 16, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer The difference is that TraceSwitch works with Trace messages whereas… May 16, 2026 at 5:50 pm

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

I am using use strict; in perl and with that I use the following
I want to use switch statement in c#. but instead of using a constant
I want to use pooled connections with Java (because it is costly to create
I'm using a SqlCommand to execute a sql statement on a SqlServer2005 db. (
If I have code with nested objects like this do I need to use
I have an sqlConnection manager class like so: public class SQLConn { public string
I am using a combination of the Enterprise library and the original Fill method
How can I execute a DDL statement via NHibernate? To be clear, I don't
Is it possible to enforce read only permissions using the System.Data.SqlClient code accessing a
I'm writing a test to see if my LINQ to Entity statement works.. I'll

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.