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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:46:58+00:00 2026-06-02T17:46:58+00:00

I have the following set of code: using (OracleConnection conn = new OracleConnection(m_fceConn)) {

  • 0

I have the following set of code:

using (OracleConnection conn = new OracleConnection(m_fceConn))
{
    conn.Open();

    using (OracleCommand cmd = new OracleCommand(m_sql, conn))
    {
        using (OracleDataReader reader = cmd.ExecuteReader())
        {
            reader.Read();
        }
    }

    conn.Close();
}

Is there a better way to format this? Granted, later on I will probably be breaking out opening a connection, running a query, and closing the connection into separate functions at a later date but this nesting will still be there “behind the scenes” so to speak.

I read on another thread that I could format things like so:

using (OracleConnection conn = new OracleConnection(m_fceConn))
using (OracleCommand cmd = new OracleCommand(m_sql, conn))

But considering that I have code in between each of these statements I don’t believe that I can omit the brackets like that. I’m just looking for the best/safest practice here since I’m still realtively new/noob to C#. Thanks.

  • 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-06-02T17:46:59+00:00Added an answer on June 2, 2026 at 5:46 pm

    I could be wrong, but I don’t think you need to open the connection before passing it to OracleCommand. You just have to open it prior to executing a command. So you could write the above as:

    using (OracleConnection conn = new OracleConnection(m_fceConn)) 
    using (OracleCommand cmd = new OracleCommand(m_sql, conn))
    { 
        conn.Open(); 
        using (OracleDataReader reader = cmd.ExecuteReader()) 
        { 
            reader.Read(); 
        } 
    }
    

    (you also shouldn’t need to explicitly close either since disposing of the connection should close it automatically).

    There’s nothing special about the above code (other than it looks nice), it’s just using normal C# rules that apply single commands to such blocks. it’s the same as this:

    if (...)
    if (...)
       dosomething(); // Look Ma, no curly braces
    

    it’s just that you’re “stacking” multiple single statements.

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

Sidebar

Related Questions

I have the following C# code using (RunspaceInvoke invoker = new RunspaceInvoke()) { invoker.Invoke(Set-ExecutionPolicy
I have the following code using SubSonic 2.1/2.2 SubSonic.Update(<some table>) .Set(<column A>) .EqualTo(<column B>).
I have using following code to set a thread on button action. public void
I have the following code using generics: Set membersKeySet = membersList.keySet(); Iterator<DoubleKey> membersItr =
I have the following code: Using cmd As SqlCommand = Connection.CreateCommand cmd.CommandText = UPDATE
I'm using socket.io and express and have the following code: io.set('authorization', function (data, accept)
I have the following code set up in my Startup IDictionary<string, string> properties =
I have the following code to set a userId variable: (userId set in prior
I have following code in my application: // to set tip - photo in
I have following tsql code in sql server 2008: declare @ID INT SET @ID

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.