David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
SqlConnection.RetrieveStatistics in .NET 2.0

The SqlConnection object has a new RetrieveStatistics method in .NET 2.0, which provides some interesting statistics that could come in handy while debugging and performance tuning your .NET applications.  Normally statistics are turned off by default, so you have to set StatisticsEnabled = true in order for the SqlConnection object to begin collecting statistics.

 

using (SqlConnection connection = 
        new SqlConnection(connectionString))
      {

        connection.StatisticsEnabled = true;

        string sql = "SELECT * FROM Products";
        SqlDataAdapter adapter = 
          new SqlDataAdapter(sql, connection);

        DataSet dataSet = new DataSet();

        adapter.Fill(dataSet, "Products");

        IDictionary statistics =
          connection.RetrieveStatistics();

        long serverRoundtrips =
            (long) statistics["ServerRoundtrips"];
        long connectionTime =
            (long) statistics["ConnectionTime"];
        long bytesReceived =
            (long) statistics["BytesReceived"];
        long sumResultSets =
            (long) statistics["SumResultSets"];
        long selectRows =
            (long) statistics["SelectRows"];
            
        // ...
        
      }

 

There are 18 different statistics gathered at the moment.  You could loop through them all as opposed to requesting one at a time:

 

foreach (DictionaryEntry entry in statistics)
{
  Console.WriteLine("Key: {0}, Value: {1}",
    entry.Key.ToString(), entry.Value.ToString());
}

 

Related Posts

 

DrinkingDragon Well Green Tea

 


Posted Wed, Nov 2 2005 9:57 AM by David Hayden
Filed under:

[Advertisement]

Comments

Jason Haley wrote Interesting Finds
on Thu, Nov 3 2005 8:40 AM