Mini Profiler integrates with SqlConnection

I have an existing db connection function in a web form application that I would like to integrate with the mini profiler . I have a mini profiler installed and running in the application, but it seems I can’t connect part of the database correctly. The following is part of the code that we associate with db c.

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 take a step around ExecuteReader () as follows:

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

but this makes the mini profiler as useful as tracing, and I want to get the query functions shown on the site. Any help?

+6
source share
2 answers

@Aghilas's answer used some other library, but was enough to point out my mistake that I did not seem to understand before.

I had to switch from using SqlConnection to using DbConnection and the same for SQLCommand => DbCommand and SQLDataReader => DbDataReader. This allowed 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; } } } } 
+1
source

You can try using this code - based on the 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

+9
source

Source: https://habr.com/ru/post/925594/


All Articles