首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从OracleCommand检索ProfiledDbCommand参数

如何从OracleCommand检索ProfiledDbCommand参数
EN

Stack Overflow用户
提问于 2016-08-02 12:57:05
回答 1查看 287关注 0票数 0

我开始使用StackExchange微型轮廓仪,并希望将它与oracle数据库一起使用。

但当我运行查询时会抛出异常-

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

我创建了新的连接:

代码语言:javascript
复制
this._oracleConnection = new OracleConnection(ConnectionString.Oracle);
this._dbConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(this._oracleConnection, MiniProfiler.Current);

方法,在其中运行查询:

代码语言:javascript
复制
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时会引发异常

代码语言:javascript
复制
void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
    var oracmd = (OracleCommand)command; // exception!
    this.AddParameters(oracmd, identity);
}

怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-03 08:55:34

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

StackExchange.Profiling.Data.ProfiledDbCommand中,似乎存在一个类型为OracleCommand的属性InternalCommand。从那里可以检索所有添加的参数。

修改代码后,它看起来如下所示:

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38721088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档