Skip to content

Oracle procedure

so edited this page May 22, 2021 · 1 revision

存储过程

Chloe 支持存储过程以及 output 参数。

通过存储过程获取一个 Person 信息: Oracle 数据库中,如果一个存储过程需要返回结果集,需要借助 RefCursor output 参数特性。用法如下:

/* 必须先自定义 RefCursor 参数 */
OracleParameter p_cur = new OracleParameter();
p_cur.ParameterName = "p_cur";
p_cur.OracleDbType = OracleDbType.RefCursor;
p_cur.Direction = ParameterDirection.Output;

DbParam refCursorParam = new DbParam();
/* 将自定义 RefCursor 参数设置到 DbParam 的 ExplicitParameter 属性 */
refCursorParam.ExplicitParameter = p_cur;

DbParam id = new DbParam("id", 1);
Person person = dbContext.SqlQuery<Person>("Proc_GetPerson", CommandType.StoredProcedure, idrefCursorParam).FirstOrDefault();

通过存储过程的 output 参数获取一个 Person 的 Name:

DbParam id = new DbParam("id", 1);
DbParam outputName = new DbParam("name", null, typeof(string)) { Direction = ParamDirection.Output };
dbContext.Session.ExecuteNonQuery("Proc_GetPersonName", CommandType.StoredProcedure, id, outputName);