【问题标题】:How to retrieve OracleCommand parameters from ProfiledDbCommand如何从 ProfiledDbCommand 中检索 OracleCommand 参数
【发布时间】:2016-08-02 12:57:05
【问题描述】:

我开始使用StackExchange mini profiler 并希望将它与 oracle 数据库一起使用。
但是当我运行查询时抛出异常 -

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

我创建新连接:

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


我运行查询的方法:

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();

    //...
}

注意: OraDynamicParams 只是一个继承自SqlMapper.IDynamicParameters 的类。

在下面的方法中,尝试强制转换为OracleCommand时会抛出异常:

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

如何解决这个问题?

【问题讨论】:

    标签: c# casting oraclecommand miniprofiler


    【解决方案1】:

    我找到了解决问题的方法。

    看来StackExchange.Profiling.Data.ProfiledDbCommand 内部有一个InternalCommand 类型为OracleCommand 的属性。从那里您可以检索所有添加的参数。

    修改代码后,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 2012-06-18
      • 1970-01-01
      • 2011-07-23
      • 2013-04-06
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多