Skip to content

Instantly share code, notes, and snippets.

@kasuken
Created October 27, 2017 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasuken/4c9d9a4b4d5177e104833095e4275729 to your computer and use it in GitHub Desktop.
Save kasuken/4c9d9a4b4d5177e104833095e4275729 to your computer and use it in GitHub Desktop.
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