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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:03:01+00:00 2026-05-11T08:03:01+00:00

If I use something like: using (OdbcConnection conn = new OdbcConnection(….)) { conn.open(); my

  • 0

If I use something like:

using (OdbcConnection conn = new OdbcConnection(....)) {   conn.open();   my commands and sql, etc. } 

Do I have to do a conn.close(); or does the using statement keep me from doing that last call? Does it dispose of everything in the using block? For example, if I called other objects unlrelated would it dipose of those automatically also?

Thank you. I was unclear after reading about using on Microsoft’s site. I want to make sure I don’t have any memory leaks.

  • 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. 2026-05-11T08:03:02+00:00Added an answer on May 11, 2026 at 8:03 am
    1. The using block will dispose of the OdbcConnection.
    2. Normal scope rules work for anything declared inside the using block.
    3. The using block will not clean up any other IDisposable objects. It only cleans up the declared item
      • note that you can nest using blocks, or if the items are the same type, multiple items can be initialized at the same time.

    See the top bit of my other answer for How do I use the using keyword in C# for a little more information.

    I should also mention that you can close (dispose) of the connection as soon as you are done with it to release the resource. The guidelines say that the caller should be able to repeatedly call the dispose method. The using block is essentially just a safety net and allows writing clearer code in most circumstances.


    [Edit] e.g. multiple initialization in a using: initialize more than one object in the same using without having to nest using blocks if the objects are the same type:

    using (Bitmap b1 = new Bitmap('file1'), b2 = new Bitmap('file2'))  { ... } 

    Joel Coehoorn mentioned stacking, which is nesting but omitting the braces, much as you can omit the braces in a for, or if statement. The UI doesn’t reformat with an indent. I’d be curious what the IL looks like.

    using(Bitmap b = new Bitmap('filex')) using(Graphics g = Graphics.FromImage(b)) {  } 

    It is an error to use put different objects in the same using error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement.

    // error CS1044 using(Bitmap b = new Bitmap('filex'), Graphics g = Graphics.FromImage(b))  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to use something like an vim-close/exit-Event to execute some last commands
I am using xampp on Windows, but I would like to use something closer
I need to use something like get_or_create() but the problem is that I have
I'm using the axis.drawGrid() method but I'd also like to use something like axis.hideGrid()
I would like to use something like this: Dictionary<int, string>[] matrix = new Dictionary<int,
which is the cleanest way to use something like a global variable? Normally, using
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use something like the following: SELECT city FROM cities WHERE
Right now we just use something like this stopWatch.Start(); try { method(); } finally
I want to use something like: ContourPlot [Abs[z-1] == 2] and to define z

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.