Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created April 9, 2014 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesuslpm/10254802 to your computer and use it in GitHub Desktop.
Save jesuslpm/10254802 to your computer and use it in GitHub Desktop.
First steps in EntityLite to support ORACLE ref cursors
using (var ds = new NorthwindDataService())
{
object cSubTree;
ds.EmployeeRepository.EmployeeSubtree(2, out cSubTree);
IDataReader reader = ((dynamic)cSubTree).GetDataReader();
foreach(var employee in reader.ToList<Employee>())
{
Console.WriteLine("{0}, {1}", employee.FirstName, employee.LastName);
}
}
public static DbCommand CreateEmployeeSubtreeProcedure(DbConnection connection, string parameterPrefix)
{
var cmd = connection.CreateCommand();
cmd.CommandText = "employee_subtree";
cmd.CommandType = CommandType.StoredProcedure;
IDbDataParameter p = null;
p = cmd.CreateParameter();
p.ParameterName = parameterPrefix + "P_EMPLOYEE_ID";
p.DbType = DbType.Decimal;
p.Direction = ParameterDirection.Input;
p.SourceColumn = "P_EMPLOYEE_ID";
cmd.Parameters.Add(p);
p = cmd.CreateParameter();
p.ParameterName = parameterPrefix + "C_SUBTREE";
Type paramType = p.GetType();
var pi = paramType.GetProperty("OracleDbType");
var refCursorValue = Enum.Parse(pi.PropertyType, "RefCursor");
pi.SetValue(p, refCursorValue, null);
p.Direction = ParameterDirection.Output;
p.SourceColumn = "C_SUBTREE";
cmd.Parameters.Add(p);
return cmd;
}
public static DbCommand CreateEmployeeSubtreeProcedure(DbConnection connection, string parameterPrefix)
{
var cmd = connection.CreateCommand();
cmd.CommandText = "employee_subtree";
cmd.CommandType = CommandType.StoredProcedure;
IDbDataParameter p = null;
p = cmd.CreateParameter();
p.ParameterName = parameterPrefix + "P_EMPLOYEE_ID";
p.DbType = DbType.Decimal;
p.Direction = ParameterDirection.Input;
p.SourceColumn = "P_EMPLOYEE_ID";
cmd.Parameters.Add(p);
p = cmd.CreateParameter();
p.ParameterName = parameterPrefix + "C_SUBTREE";
p.DbType = DbType.Object;
p.Direction = ParameterDirection.Output;
p.SourceColumn = "C_SUBTREE";
cmd.Parameters.Add(p);
return cmd;
}
CREATE OR REPLACE PROCEDURE employee_subtree(
p_employee_id IN BINARY_INTEGER,
c_subtree OUT SYS_REFCURSOR
)
IS
BEGIN
OPEN c_subtree FOR
SELECT
employee_id, reports_to, first_name, last_name
FROM
employees
CONNECT BY PRIOR employee_id = reports_to
START WITH employee_id = p_employee_id;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment