0

I am using "using" Keyword in c#.

using(SqlConnection sourceConnection = new SqlConnection())
{
   sourceConnection.Open(); 
   var destinationConnection = new SqlConnection();
   destinationConnection.Open();

}

In Above line of code is both the connections will dispose or only sourceConnection() will dispose.

Mayank Jain
  • 225
  • 2
  • 6

4 Answers4

3

Only sourceConnection, i.e. the object that you wrap in the using statement, will be disposed. You should use another using statement to make sure that destinationConnection is also disposed:

using (SqlConnection sourceConnection = new SqlConnection())
using (var destinationConnection = new SqlConnection())
{
   sourceConnection.Open(); 
   destinationConnection.Open();

}
mm8
  • 163,881
  • 10
  • 57
  • 88
3

using operates on the IDisposable interface. Ignoring the scoping of the variable, it basically does something a bit like this:

SqlConnection sourceConnection = new SqlConnection();
try
{
   sourceConnection.Open(); 
   var destinationConnection = new SqlConnection();
   destinationConnection.Open();
}
finally
{
    if (sourceConnection != null)
    {
        sourceConnection.Dispose();
    }
}

So in answer to your question, it will only close one.

From MSDN via this post by Robert S.:

C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

Again this indicates that it only works on one object: the subject of the using.

Community
  • 1
  • 1
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
2

Only one of them will be disposed, you're never explicitly calling Dispose or wrapping in a using the destinationConnection.

You could do this, and both will always dispose.

using(SqlConnection sourceConnection = new SqlConnection())
{
   using(SqlConnection destinationConnection = new SqlConnection())
   {
   }
}
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

As some people already noted, using wraps the IDisposable interface, as in:

using (var foo = new MyDisposable()) { ... } 

is the same as:

var foo = new MyDisposable()
try { 
    ... 
}
finally { 
    if (foo != null) { foo.Dispose(); }
}

So far so good. The reason that C# implements this is because of unmanaged resources. Why? Unmanaged resources require you to clean up the stuff at a time you want, instead of a time that the Garbage Collector (GC) wants (which is f.ex. when you run out of memory).

Consider the alternative for a moment: let's assume you have a file. You open the file, write some bytes, forget 'close' and then don't have IDisposable to close it. Your file will continue to be open, even though you don't use the object anymore. Even worse, you don't even know that data has been written if your program exits. If your program runs long enough, at some point the GC will probably kick in and remove the thing for you, but until then every other attempt to open the file will probably give you a big, fat error. So, in short...: a lot of pain and misery.

And this is what IDisposable solves.

Connections, files, memory access, network access, ... basically everything that uses stuff that needs to be cleaned up implements IDisposable. It even holds true that if a type implements IDisposable, you'd better Dispose it.

So... SQL connections implement IDisposable, SQL Readers implement IDisposable, and so forth. Personally, I tend to check every type for the presence of an IDisposable interface, before working with it (so yeah that is: all the time).

Once you understand this, the correct way to use all this is obvious:

using (var sourceConnection = new SqlConnection())
{
    sourceConnection.Open(); 
    using (var destinationConnection = new SqlConnection())
    {
        destinationConnection.Open();

        // ...
        using (var myReader = srcConnection.ExecuteReader(...))
        {
            // ...
        }
    }
}

... and so on.

Now, in some cases you obviously cannot use using because you're using different methods. How to solve this? Easy: Implement the Dispose Pattern in the class that has these methods.

More information (as well as the Dispose pattern): https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx

atlaste
  • 30,418
  • 3
  • 57
  • 87