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


 

This entry was posted in ADO.NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>