Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Retrieve the SQL Server Statistics for a SQL Command
public Dictionary<string,string> Run(string connectionString, string query)
{
using (var sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.StatisticsEnabled = true;
sqlConnection.Open();
using (var cmd = new SqlCommand(query, sqlConnection))
{
cmd.ExecuteReader().Dispose();
}
var stats = sqlConnection.RetrieveStatistics();
var statistics = new Dictionary<string, string>();
foreach (DictionaryEntry entry in stats)
{
statistics.Add(entry.Key.ToString(), entry.Value.ToString());
}
sqlConnection.ResetStatistics();
return statistics;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment