Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Glimpse provide diagnostics when using the SqlClient namespace classes

I've downloaded Glimpse and the Glimpse.ADO extension and installed it on my test instance.

I thought I'd get a capture of any sql that was executed, but it seems like it doesn't capture commands with the way our code is written.

        using (var conn = new SqlConnection(cString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "Select count(*) from table";
            cmd.CommandType = CommandType.Text;

            txtResult2.Text = cmd.ExecuteScalar().ToString();
            conn.Close();
        }

I CAN get it to provide information from a test page with the sql code written like so:

        var factory =DbProviderFactories.GetFactory(cString.ProviderName);
        using (var connection = factory.CreateConnection())
        {
            connection.ConnectionString = connectionString.ConnectionString;
            connection.Open();

            using (var command = connection.CreateCommand())
            {
                command.CommandText = "SELECT COUNT(*) FROM table";
                command.CommandType = CommandType.Text;
                txtResult1.Text = command.ExecuteScalar().ToString();
            }
        }

However I have too many places in my code to change if I can only capture data using this dbProviderFactories method.

Is there a way to get Glimpse.ADO to work with the System.Data.SqlClient.SqlConnection class? Is there another Glimpse extension that works with this namespace?

Is there another way to tackle this problem?

like image 396
Aheho Avatar asked Apr 29 '13 18:04

Aheho


1 Answers

I agree with @StriplingWarrior, leveraging the provider factories will make your code more DRY and follow best practices. DbProviderFactories really is the best way to do this and your code won't explicitly rely on Glimpse.

However, if you really want to just move forward with your existing app code, Glimpse will support you with the following changes:

using (var conn = new GlimpseDbConnection(new SqlConnection(cString))
{
    conn.Open();
    DbCommand cmd = conn.CreateCommand();
    cmd.CommandText = "Select count(*) from table";
    cmd.CommandType = CommandType.Text;

    txtResult2.Text = cmd.ExecuteScalar().ToString();
    conn.Close();
}

In the example above, the command is created with the CreateCommand() method, which removes the need to associate the command and the connection.

Alternatively, you could also still explicitly create the command like so:

conn.Open();
DbCommand cmd = new GlimpseDbCommand(new SqlCommand());
cmd.Connection = conn;
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;

Finally, more documentation about the SQL tab is available by clicking the ? icon in the Glimpse UI when you have the tab selected, or by going to our SQL documentation on getGlimpse.com. (I'll be adding this info to that page for future reference.)

like image 92
nikmd23 Avatar answered Oct 06 '22 03:10

nikmd23