2012-09-17 8 views
6

私はmini profilerと統合したいWebフォームアプリケーションに既存のdb接続関数を持っています。私はミニプロファイラーをインストールして、アプリケーションで実行しているが、私はデータベース部分が正しく接続されるように見えることはできません。以下はdbに接続するコードの一部です。Mini ProfilerはSqlConnectionと統合します

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; 
       } 
      } 
     } 
    } 
} 

私は簡単そうのような)のExecuteReader(周りのステップを置くことができます。

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

が、これは、トレースなどとして有用な程度のミニプロファイラを行い、私は、クエリは上の示された特徴を取得したいと思っていますサイト。どんな助け?

答えて

1

@aghilasの答えはいくつかの他のライブラリを使用していたが、私は思えなかったことを私のミスを指摘してenoughtました前に把握する。

SqlConnectionを使用することからDbConnectionを使用すること、そしてSQLCommand => DbCommand、およびSQLDataReader => DbDataReaderに変更する必要がありました。これにより、 ProfiledDbConnectionが正しく接続されました。

... 
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; 
       } 
      } 
     } 
    } 
9

あなたはこのコードを試すことができます - ProfiledDbConnectionクラス

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

リンクに基づいて:https://github.com/ServiceStack/ServiceStack/tree/master/src/ServiceStack/MiniProfiler/Data

+1

この作業を試みても、「ミニプロファイラにはデータの定義が含まれていません」と表示されます。 – ThunD3eR

関連する問題