current state: Open, before: Closed
connection opened
warning or info: Invalid object name 'ProCSharp.Books'.
Statistics
BuffersReceived, value: 1
BuffersSent, value: 1
BytesReceived, value: 178
BytesSent, value: 124
CursorOpens, value: 0
IduCount, value: 0
IduRows, value: 0
PreparedExecs, value: 0
Prepares, value: 0
SelectCount, value: 0
SelectRows, value: 0
ServerRoundtrips, value: 1
SumResultSets, value: 0
Transactions, value: 0
UnpreparedExecs, value: 1
ConnectionTime, value: 88
ExecutionTime, value: 7184
NetworkServerTime, value: 11
current state: Closed, before: Open
completed
 
    public static void ConnectionInformation()
        {
            using (var connection = new SqlConnection(GetConnectionString()))
            {
                connection.InfoMessage += (sender, e) =>
                {
                    Console.WriteLine($"warning or info: {e.Message}");
                };
                connection.StateChange += (sender, e) =>
                {
                    Console.WriteLine($"current state: {e.CurrentState}, before: {e.OriginalState}");
                };
                try
                {
                    connection.StatisticsEnabled = true;
                    connection.FireInfoMessageEventOnUserErrors = true;
                    connection.Open();
                    Console.WriteLine("connection opened");
                    // Do something useful
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT Titl, Publisher FROM [ProCSharp].[Books]";
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine($"{reader.GetString(0)} {reader.GetString(1)}");
                    }
                    IDictionary statistics = connection.RetrieveStatistics();
                    ShowStatistics(statistics);
                    connection.ResetStatistics();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }