Created
October 27, 2017 11:55
-
-
Save kasuken/4c9d9a4b4d5177e104833095e4275729 to your computer and use it in GitHub Desktop.
Retrieve the SQL Server Statistics for a SQL Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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