Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mini Profiler integrate with SqlConnection

I have an existing db connection function in a web forms app that I would like to integrate with mini profiler. I have mini profiler installed and running on the app, but I cannot seem to get the database portion connected properly. Below is part of the code we connect to the db with.

public override IEnumerable<IDataRecord> Execute()
{
    using( SqlConnection conn = new SqlConnection( ConnectionString ) ) {
        using( SqlCommand command = new SqlCommand( CommandText, conn ) ) {
            command.CommandType = SQLCommandType;
            foreach( SqlParameter p in ParamsToAdd ) {
                command.Parameters.Add( p );
            }
            conn.Open();
            SqlDataReader rdr;
            try {
                rdr = command.ExecuteReader();
            } catch( Exception ex ) {
                //log error
            }
            using( rdr ) {
                while( rdr.Read() ) {
                    yield return (IDataRecord)rdr;
                }
            }
        }
    }
}

I can easily put a step around the ExecuteReader() like so:

using( MiniProfiler.Current.Step( command.CommandText ) ) {
    rdr = command.ExecuteReader();
}

but this makes the mini profiler about as useful as trace and I am wanting to get the query features shown on the site. Any Help?

like image 313
corymathews Avatar asked Sep 17 '12 16:09

corymathews


2 Answers

You can try with this code - based on ProfiledDbConnection class

var connection = MiniProfiler.Data.ProfiledDbConnection.Get(new SqlConnection(str));
var cmd = connection.CreateCommand();
var param = connection.CreateParameter(); 

Link : https://github.com/ServiceStack/ServiceStack/tree/master/src/ServiceStack/MiniProfiler/Data

like image 122
Aghilas Yakoub Avatar answered Sep 18 '22 01:09

Aghilas Yakoub


@aghilas answer was using some other library but was enought to point out my mistake that I could not seem to figure out before.

I ended up having to change from using SqlConnection to using DbConnection and the same for SQLCommand => DbCommand, and SQLDataReader => DbDataReader. This allowed the ProfiledDbConnection to connect properly.

...
using StackExchange.Profiling;
using StackExchange.Profiling.Data;
...
using( DbConnection conn = new ProfiledDbConnection( new SqlConnection( ConnectionString ), MiniProfiler.Current ) ) {
        using( DbCommand command = conn.CreateCommand() ) {
            command.CommandText = CommandText;
            command.Connection = conn;
            command.CommandType = SQLCommandType;
            foreach( SqlParameter p in ParamsToAdd ) {
                command.Parameters.Add( p );
            }
            conn.Open();
            DbDataReader rdr;
            try {
                rdr = command.ExecuteReader();
            } catch( Exception ex ) {
                //log error
            }
            using( rdr ) {
                while( rdr.Read() ) {
                    yield return (IDataRecord)rdr;
                }
            }
        }
    }
like image 37
corymathews Avatar answered Sep 19 '22 01:09

corymathews