As i was just about to code a logging system for a project, i remembered a couple of fine methods which SqlConnection class has.
RetrieveStatistics();
It works with first enabling the stats mode, resetting the stats and when the command is executed, finally retrive the stats.
string strConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=FooDB;Integrated Security=True";
using (SqlConnection conSQL = new SqlConnection(strConnectionString))
{
using (SqlCommand cmdSQL = new SqlCommand("select * from Foo", conSQL))
{
cmdSQL.CommandType = CommandType.Text;
try
{
conSQL.Open();
conSQL.StatisticsEnabled = true;
conSQL.ResetStatistics();
SqlDataReader drdActiveConfiguration = cmdSQL.ExecuteReader();
IDictionary idcStat= conSQL.RetrieveStatistics();
foreach (DictionaryEntry dceItem in idcStat)
{
Console.WriteLine("Key: {0}, Value: {1}", dceItem.Key.ToString(), dceItem.Value.ToString());
}
DataSet dsActiveConfiguration = new DataSet();
dsActiveConfiguration.Load(drdActiveConfiguration, LoadOption.OverwriteChanges, new string[] { "Foo1" });
dsActiveConfiguration.Clear();
conSQL.Close();
}
catch (Exception excp)
{
}
}
}
By using this method, you get a quite information about the execution process...
Key: NetworkServerTime, Value: 375
Key: BytesReceived, Value: 224
Key: UnpreparedExecs, Value: 1
Key: SumResultSets, Value: 0
Key: SelectCount, Value: 0
Key: PreparedExecs, Value: 0
Key: ConnectionTime, Value: 390
Key: ExecutionTime, Value: 390
Key: Prepares, Value: 0
Key: BuffersSent, Value: 1
Key: SelectRows, Value: 0
Key: ServerRoundtrips, Value: 1
Key: CursorOpens, Value: 0
Key: Transactions, Value: 0
Key: BytesSent, Value: 388
Key: BuffersReceived, Value: 1
Key: IduRows, Value: 0
Key: IduCount, Value: 0
Press any key to continue . . .which is very handy :)