ado.netdb2miniprofiler

Profiling DB2 connection with MiniProfier


I'm trying to add MiniProfiler to my DB2 connections. Below is my simplified code.

public void InitializeConnection()
{
    DB2Connection cnn = new DB2Connection("connection String");
    var profiler = 
         new StackExchange.Profiling.Data.ProfiledDbConnection(cnn, MiniProfiler.Current);
    IDbCommand c = new DB2Command();
    c.Connection = profiler ;
}

My problem is occurring in the last line where the profiler is assigned to the DB2Command's Connection property. I'm getting the below error.

Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbConnection' to type 'IBM.Data.DB2.DB2Connection' I've tried a couple of different casting ideas and nothing has worked out.


Solution

  • I think you're going at it backwards. You're assigning the connection to the ProfiledDbConnection class (as seems to be correct, based on the docs on the MiniProfiler website).

    However, you're then creating a DB2-specific command object, and trying to assign the ProfiledDbConnection class to the connection object.

    I think what you want to do is call profiler.CreateDbCommand(), which will create a ProfiledDbCommand object that uses the DB2Command class "under the covers".