-3

I wonder that what is the difference between ;

using (var con = new OracleConnection(oradb))
            {
                con.Open();
                // Do sth.
                con.Dispose();
                con.Close();
            }

from this;

oradb = ConString;
OracleConnection con = new OracleConnection(oradb))
con.Open();
// Do Sth.
con.Dispose();
con.Close();

Both of them works for me, However didn't understand the basic behind of this.

Which one should i use ?

john true
  • 263
  • 1
  • 5
  • 26
  • 2
    Using handles create/destroy – BugFinder Nov 27 '17 at 14:04
  • You dont have to dispose it... thats what the using is for – EpicKip Nov 27 '17 at 14:04
  • The using ensures that the connection gets disposed even in case of error. You need a `try...catch...finally` to achieve the same. So it's a less verbose version. The `con.Close` and `con.Dispose` are redundant because that is what the using statement does for you(`Dispose` will close the connection). – Tim Schmelter Nov 27 '17 at 14:05
  • 1
    difference between `stackoverflow & google.com` – MethodMan Nov 27 '17 at 14:07
  • `Close` will call `Dispose`. So in your first example with `using` you´re effectivly calling `Dispose` disposing three times, once directly, once by `Close` and last time when leaving the `using`-block. See also https://stackoverflow.com/questions/61092/close-and-dispose-which-to-call – MakePeaceGreatAgain Nov 27 '17 at 14:13

4 Answers4

1

using with dispose your connection object automatically. you dont have to do it manually.

using(var con = new OracleConnection(oradb))
{
  con.Open();
}

is equal to (some what similar , you can use ILDSAM on your code and check)

OracleConnectioncon = null;
try
{
  con = new OracleConnection(oradb);
  con.Open();
}
finally
{
  con.Disponse();
}

you can clear you concept by reading : Dispose Pattern

Cleptus
  • 3,446
  • 4
  • 28
  • 34
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

Using ensures that the connection is always disposed. If you dispose it manually it can happen that part of the code is never executed at all.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
1

With using, disposing of connection is handled automaticaly :

using (var con = new OracleConnection(oradb))
        {
            con.Open();
            // Do sth.
            con.Close();
        } // ok
Pac0
  • 21,465
  • 8
  • 65
  • 74
1

If we look at the decompiled code:

using System;
public class C {
    public void M() {
        using(var conn = new Connection()){
        }
    }
}

is turned into:

public class C
{
    public void M()
    {
        Connection connection = new Connection();
        try
        {
        }
        finally
        {
            if (connection != null)
            {
                ((IDisposable)connection).Dispose();
            }
        }
    }
}

Unlike the code without a using block, the connection will be disposed even if the exception is thrown.

A bit more reading about the using could be found here (C# Reference doc)

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39