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?
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
@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;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With