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 5975205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:03:02+00:00 2026-05-22T21:03:02+00:00

Does placing a SqlCommand.Connection in a using block close the connection in the same

  • 0

Does placing a SqlCommand.Connection in a using block close the connection in the same way as placing the SqlConnection in it’s own using block? (Is #1 = #2)

Example 1

using (var cmd = GetCommandWithConnectionSetInternally(connString))
{ 
    using (cmd.Connection) 
    {

    }
}

Example 2

using(var conn = new SqlConnection(connString))
{
     using(var cmd = new SqlCommand(cmdText, conn))
     {

     }
 }

So, does the connection in Example 1 get closed as it would in Example 2 upon exiting the “using” block?

  • 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-22T21:03:03+00:00Added an answer on May 22, 2026 at 9:03 pm

    I can’t see it working any other way since using is just a short form of try {} finally {} with a Dispose call in the finally block. Since the resource object being used is cmd.Connection, it should get disposed.

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

Sidebar

Related Questions

In PHP switch statements, does placing more common cases near the top improve performance?
Possible Duplicates: What does placing a @ in front of a C# variable name
can use anything in any order? does placing of <meta http-equiv=Content-Type content=text/html;charset=UTF-8> is important
First the question: I need a way without placing a config file in /etc/ld.so.conf.d
I'm finding my way through a 10000-file codebase by placing breakpoints and I'm expected
Does something like this affect performance badly? var myQuery = from c in Customers
I'm using an older version of ASP.NET AJAX due to runtime limitations, Placing a
In my application iam using Log4j for logging.Presently I am placing log4j.xml in WEB-INF/classes.
Possible Duplicate: What does placing a @ in front of a C# variable name
I cannot figure this one out. When using display:table along with display:table-cell , placing

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.