Click here to Skip to main content
12,997,060 members (40,879 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.5K views
Posted 1 Nov 2011

SqlConnection.Dispose internals

, 1 Nov 2011
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

zenwalker1985
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 1 Nov 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid