Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

SqlConnection.Dispose internals

0.00/5 (No votes)
1 Nov 2011LGPL31 min read 5.4K  
SqlConnection.Dispose internals

Today, I was just looking at the IL of SqlConnection class, so I found few things which are interesting to me and hopefully to you as well.

  1. SqlConnection.Dispose() shall call Close() method internally, so if you’re using Using(){} block to open a connection, then you need not Close it explicitly.
  2. SqlConnection.Dispose() method also calls SqlConnection.DisposeMe(bool) method internally, although SqlConnection.DisposeMe(bool) method is empty, but this method is private to this class. The reason I guess they have done this way is for futuristic purposes.

There are quite a few differences I could spot out in the IL with respect to Dipose() and Close() methods:

  1. Close() method is actually an abstract method in DBConnection class. It is overridden and made virtual in SqlConnection class, which gives the consumer code more flexibility in customizing the Close operation if at all needed.
  2. There is no Dispose() method in DBConnection class but instead it's a virtual method in SqlConnection class.
  3. Close() method actually only closes the connection object via Native interface with the DB and tries to clean it up. Internally, it uses DBConnectionInternal.CloseConnection method to actually close the transaction with the DB by using many APIs.
  4. On the other hand, Dispose() method will not close any connection explicitly, but calls Close() method. Along with it, it also calls DisposeMe() method internally.

Happy coding! :)

P.S: Your comments/votes are much appreciated. :)

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)