Can Glimpse provide diagnostics when using the SqlClient namespace classes
Asked Answered
B

2

7

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?

Berzelius answered 29/4, 2013 at 18:38 Comment(2)
Just an observation: if there are too many places in your code that create a connection in a specific way, it may be a sign that your code is too repetitive.Stock
Perhaps, but as someone smarter than I said, "Wherever you are, that's where you'll have to start"Berzelius
O
6

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.)

Ostensible answered 30/4, 2013 at 9:52 Comment(2)
What namespace is GlimpseDbConnection declared in?Berzelius
Glimpse.Ado.AlternateType its in the Glimpse.Ado package.Grapery
O
1

In problems section on Glimpse site...

Getting Glimpse to work with manual created SQL Connections/Commands

http://getglimpse.com/Docs/Manual-ADO-Integration

Obedient answered 7/9, 2015 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.