Kittoes0124
Kittoes0124

Reputation: 5080

Proper format of nested "using" statements?

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.

Upvotes: 4

Views: 1289

Answers (4)

therealmitchconnors
therealmitchconnors

Reputation: 2760

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

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

    conn.Close();
}

EDIT On second thought, don't do this. It wouldn't dispose of the command object. I will leave the answer here as an example of what NOT to do.

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

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.

Upvotes: 3

Eric Andres
Eric Andres

Reputation: 3417

Your first using statement needs to have brackets, since you have the conn.Open() call there. The second and third can be stacked as you stated, which is pretty idiomatic C# (and preferred, in my case).

Upvotes: 2

Adam Robinson
Adam Robinson

Reputation: 185643

What you have is properly formatted, though it's not necessary to call conn.Close() since the connection will be disposed (and thus closed) at the end of the using block that declared it.

You can, however, omit the brackets for the second statement, since the entirety of that statement is just another using block.

Upvotes: 2

Related Questions