流利的NHibernate Oracle功能
本文关键字:Oracle 功能 NHibernate | 更新日期: 2023-09-27 18:12:10
我目前正在使用Fluent NHibernate ORM,并试图调用oracle函数fu_GetUserGrant
。我试过下面的代码:
var dbCommand = session.Connection.CreateCommand();
dbCommand.CommandText = "select fu_GetUserGrant(@grantId) from dual;";
dbCommand.CommandType = CommandType.Text;
var param = dbCommand.CreateParameter();
{
param.Value = grantId;
param.DbType = DbType.StringFixedLength;
param.Size = 200;
param.ParameterName = "@grantId";
}
dbCommand.Parameters.Add(param);
var result = dbCommand.ExecuteNonQuery();
return long.Parse(result.ToString());
And get exception - Oracle.ManagedDataAccess.Client.OracleException : ORA-00936: missing expression
几个小时失败后,我尝试了另一种方法:
var c = session.
CreateQuery("select fu_GetUserGrant(:grantId) from dual;")
.SetParameter("grantId", grantId).UniqueResult<int>();
并得到异常- NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : dual is not mapped [select fu_GetUserGrant(:grantId) from dual;]
在第一个示例中,您使用SQL查询,在第二个HQL查询。在HQL查询中当然没有双重。所以你应该做的是在SQL中使用":"代替"@"
dbCommand.CommandText = "select fu_GetUserGrant(:grantId) from dual;"
我终于找到了问题的答案。
使用下面的代码调用oracle函数:
var dbCommand = session.Connection.CreateCommand();
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.CommandText = "fu_GetUserGrant";
var returnValue = dbCommand.CreateParameter();
{
returnValue.ParameterName = "Return_Value";
returnValue.DbType = DbType.Decimal;
returnValue.Direction = ParameterDirection.ReturnValue;
}
var grantIdParam = dbCommand.CreateParameter();
{
grantIdParam.Value = grantId;
grantIdParam.DbType = DbType.StringFixedLength;
grantIdParam.Size = 200;
grantIdParam.ParameterName = "m_In_GrantId";
grantIdParam.Direction = ParameterDirection.Input;
}
dbCommand.Parameters.Add(returnValue);
dbCommand.Parameters.Add(grantIdParam);
dbCommand.ExecuteReader();
return long.Parse(returnValue.Value.ToString());
我学到的是,您可以通过CommandType.StoredProcedure;
调用oracle函数,如果您指定将从函数返回值的位置,在这种情况下是- var returnValue ...