Search code examples
c#castingoraclecommandminiprofiler

How to retrieve OracleCommand parameters from ProfiledDbCommand


I started using the StackExchange mini profiler and wanted to use it with oracle database.
But the exception is thrown when I run the query-

Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbCommand' to type 'Oracle.ManagedDataAccess.Client.OracleCommand'.

I create new connection:

this._oracleConnection = new OracleConnection(ConnectionString.Oracle);
this._dbConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(this._oracleConnection, MiniProfiler.Current);


Method in which I run the query:

private long InsertData(SomeModel model, long someId, DbConnection conn)
{
    OraDynamicParams insrtParams = this.GetSomeParams(model);
    insrtParams.Add("a_some_id", someId);
    insrtParams.Add("cur_OUT", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output);
    dynamic res = conn.Query("SOME_CRUD.INSERT_PROC", insrtParams, commandType: CommandType.StoredProcedure).First();

    //...
}

Note: OraDynamicParams is simply a class that inherits from SqlMapper.IDynamicParameters.

In following method the exception is thrown when try to cast to OracleCommand:

void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
    var oracmd = (OracleCommand)command; // exception!
    this.AddParameters(oracmd, identity);
}

How to fix this?


Solution

  • I found the solution for my problem.

    It appears that inside the StackExchange.Profiling.Data.ProfiledDbCommand there is a property InternalCommand that is of type OracleCommand. From there you can retrieve all added parameters.

    After modifying the code, it looks something like this:

    void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
    {
         // Checks if profiler is used, if it is, then retrieve `InternalCommand` property
         dynamic oracmd = command is OracleCommand ? command : ((ProfiledDbCommand)command).InternalCommand;
         this.AddParameters((OracleCommand)oracmd, identity);
    }