Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Manual ADO Integration

MikeSQ edited this page Jul 16, 2014 · 2 revisions

To retrieve data for the SQL tab, Glimpse attaches itself by providing its own GlimpseDbProviderFactory which wraps the standard ones. This lets it collect information about how you're using your data connections.

If you're not using an ADO connection that uses the DbProviderFactories class, Glimpse won't collect any data for the SQL tab (out of the box). You can fix this by manually providing a Glimpse-wrapped IDbConnection.

Setting up Glimpse with your ADO connection when using a data context

First, find the location where your connection or data context is created. If you're using Linq to SQL, you'll find it in the .designer.cs file for your dbml.

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MyApp")]
public partial class MyAppDataContext : System.Data.Linq.DataContext
{
    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
         
    #region Extensibility Method Definitions
         // snip
    #endregion
 
    public MyAppDataContext(string connection) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }
 
    public MyAppDataContext(System.Data.IDbConnection connection) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }
 
    // snip
}

You can see two overloaded constructors, one of which takes an IDbConnection. We want to use this constructor, passing in our IDbConnection wrapped in a GlimpseDbConnection.

For example, if you're using the overload with the connection string you need to change code like this:

var context = new MyAppDataContext(myConnectionString);

to this:

var context = new MyAppDataContext(
    new GlimpseDbConnection(
        new SqlConnection(myConnectionString)
    )
);

All that's happening is that you're manually wrapping your connection in a GlimpseDbConnection. In short, if you have control over the creation of the IDbConnection, you can get Glimpse working with very little fuss. Of course, you'll need to include Glimpse.Ado in the project that calls this constructor.

Setting up Glimpse with a plain ADO connection

If you are not using an abstracted data context, ensure that all queries are constructed with DbProviderFactories. If you are using a SqlConnection, Glimpse will not be able to record the query.

Changing from SqlConnection is fairly straight-forward. Ensure that you include the appropriate namespace

using System.Data.Common;

Change code that might look like the following:

using (SqlConnection connection = new SqlConnection(myConnectionString))
{
    using (SqlCommand command = new SqlCommand("SomeSqlStuff", connection))
    {
        command.CommandType = CommandType.StoredProcedure;
        //do some work
    }
}

Into code that looks more like:

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); //If not using SqlClient, replace as necessary
using (var connection = factory.CreateConnection())
{
    using (var command = factory.CreateCommand())
    {
        connection.ConnectionString = myConnectionString;
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "SomeSqlStuff";

        var parameters = new[]{
             new SqlParameter(){ ParameterName="@var1", Value = var1 },
             new SqlParameter(){ ParameterName="@var2", Value = var2 }
        };
        command.Parameters.AddRange(parameters);

        //do some work
    }
}

You will need to change any provider-specific entities into the DbProviderFactory equivalents, such as converting SqlDataReader into DbDataReader.

If using a SqlDataAdapter, you will need to use a factory method to obtain an equivalent Data Adapter such as

using (var da = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateDataAdapter())
{
    // Do some work with the data adapter
}