给定这个 oracle 存储的 proc 包:
CREATE OR REPLACE
PACKAGE TESTPKG AS
FUNCTION TestReturnRefCursor RETURN SYS_REFCURSOR;
PROCEDURE TestingRefCursorsAsOutParam (outp OUT SYS_REFCURSOR);
END TESTPKG
而这个包体:
CREATE OR REPLACE
PACKAGE BODY TESTPKG AS
FUNCTION TestReturnRefCursor RETURN SYS_REFCURSOR AS
outp SYS_REFCURSOR;
BEGIN
OPEN outp FOR SELECT * FROM TABLENAME;
RETURN outp;
END TestReturnRefCursor;
PROCEDURE TestingRefCursorsAsOutParam (outp OUT SYS_REFCURSOR) AS
BEGIN
OPEN outp FOR SELECT * FROM TABLENAME;
END TestingRefCursorsAsOutParam;
END TESTPKG;
我正在尝试使用 Dapper 调用任一函数。我创建了实现 Dapper.SqlMapper.IDynamicParameters 的 OracleDynamicParameters 类,我在搜索 StackOverflow 后在这篇文章中找到了该类:https ://gist.github.com/vijaysg/3096151
使用以下代码调用 TestReturnRefCursor 存储函数可以正常工作:
var conn = new OracleConnection(ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString);
conn.Open();
var p = new OracleDynamicParameters();
p.Add("retSet", dbType: OracleDbType.RefCursor, direction: ParameterDirection.ReturnValue);
conn.Execute("TESTPKG.TestReturnRefCursor ", param: p, commandType: CommandType.StoredProcedure);
var refcur = p.Get<OracleRefCursor>("retSet");
// do something with refcur
conn.Close();
但是,当我尝试使用以下代码调用 TestingRefCursorsAsOutParam 存储过程时:
var conn = new OracleConnection(ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString);
conn.Open();
var p = new OracleDynamicParameters();
p.Add("retSet", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output);
conn.Execute("TESTPKG.TestingRefCursorsAsOutParam ", param: p, commandType: CommandType.StoredProcedure);
var refcur = p.Get<OracleRefCursor>("retSet");
// do something with refcur
conn.Close();
我从 Oracle.ManagedDataAccess.dll 得到一个 OracleException 的详细信息:
"ORA-06550: line 1, column 7:\nPLS-00306: wrong number or types of arguments in call to 'TESTINGREFCURSORSASOUTPARAM'\nORA-06550: line 1, column 7:\nPL/SQL: Statement ignored"
我在搜索另一个 stackoverflow 帖子后发现,有时参数如果为 null 则不会绑定,除非您专门将它们的值设置为 DBNull.Value,所以我尝试将添加参数行更改为:
p.Add("retSet", value: DBNull.Value, dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output);
但这没有帮助。
我可以使用常规的 c# oracle 代码调用这个存储过程:
var cmd = conn.CreateCommand();
cmd.CommandText = "TESTPKG.TestingRefCursorsAsOutParam";
cmd.CommandType = CommandType.StoredProcedure;
var outparam = new OracleParameter();
outparam.ParameterName = "retSet";
outparam.Direction = ParameterDirection.Output;
outparam.OracleDbType = OracleDbType.RefCursor;
cmd.Parameters.Add(outparam);
OracleDataAdapter da = new OracleDataAdapter(cmd);
cmd.ExecuteNonQuery();
var refcur = (OracleRefCursor)cmd.Parameters[0].Value;
我在这里遗漏了什么,还是 Dapper 无法正常运行?