C# · CodeProject · Dotnet

SqlConnection.Dispose internals

Dear Reader,

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 to 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 the futuristic purpose.

There are quite few difference i could spot out in the IL w.r.t Dipose() and Close() methods:

  1. Close() method is actually abstract method in DBConnection class. It is overriden and made virtual in SqlConnection class, which gives the consumer code more flexibility in customizing the Close operation if at all if needed.
  2. There is no Dispose() method in DBConnection class but instead its 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 πŸ™‚

Advertisements

One thought on “SqlConnection.Dispose internals

  1. Since you have seen the IL codes of the SqlConnection , i think i can get some idea from you for my task.

    I am trying to capture the Db query and measure the time taken for execution for this below piece of C# .net Web application.

    There are way to do if i use DbProviderfactory by making a proxy of it. But instead of using a DbProviderFactory if i use SqlConnection and SqlCommand directly like below(direct ADO.Net) , how can i trace my DB calls ?

    SqlConnection myConn = new SqlConnection(@”Server=192.168.23.99;Initial Catalog=cat1;User ID=user21;Password=userpwd”);
    SqlCommand myCommand = new SqlCommand(“select * from table1”, myConn);
    SqlDataReader dataReader;

    System.Threading.Thread.Sleep(5000);
    try
    {
    myConn.Open();
    dataReader = myCommand.ExecuteReader();

    GridView1.DataSource = dataReader;
    GridView1.DataBind();
    dataReader.Close();
    myCommand.Dispose();
    myConn.Close();
    }
    catch (System.Exception ex)
    {
    Response.Write(ex.ToString());
    }

    I am aware that there are profilers to trace this. But , I want to do this in my program of my own.

    Basically i need to profile the Db Connection and Db commands in a web request, Which does not use DbProviderFactories in it. I am new in developing profilers for .Net , so i expect some guidelines to do this.

    Thanks for reading.!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s