从 ASP.NET Core 中的 Oracle 存储过程返回数据列表

问题描述 投票:0回答:1

为了获得经理批准,我在 Oracle 中创建了一个存储过程来获取具有 ID 和姓名的员工列表。然后我在 ASP.NET Core 控制器中调用它,但它只返回最后一名员工,而不是整个列表。当我尝试将其作为游标调用时,它不返回任何内容。

create or replace PROCEDURE MANGERLIST
(
  EMPNO IN NUMBER 
, DG IN NUMBER 
, DEPT IN NUMBER 
, DESGN IN NUMBER 
,l_name OUT sys_refcursor
) AS 

  l_sql VARCHAR2(800);

BEGIN
  l_sql := 'select TRIM(emp_no) , TRIM(EMP_NAME_E) FROM hr."VempDtls" where DESG_TYPE <>99 ';

if DESGN in (4,5,6,99) then
    if EMPNO = 213 then
      l_sql := l_sql || 'and emp_no =85 ' ;    
    ELSif EMPNO in (341,215,278) then
        l_sql := l_sql||' and emp_no in(213,215,219,58) ';    
    ELSif EMPNO in (336,323) then
      l_sql := l_sql||' and emp_no in(277,219,58) ';    
    ELSE  
     l_sql := l_sql||' and (dg_code = DG and DESG_TYPE in (2,3,0,1) and emp_no <>EMPNO) OR (dg_code = DG  and dept_code = DEPT
            and DESG_TYPE in (4,5,6) and emp_no <>EMPNO and DESG_TYPE < DESGN) ';    
    end if;
 ELSif DESGN in (2,3) and EMPNO != 58 then 
       l_sql := l_sql||' and emp_no=120 ';
 else
      l_sql := l_sql;
end if;

   OPEN  l_name FOR l_sql;

END MANGERLIST;

这是我的 C# 类:

public class MangerProc
{
    [NotMapped]
    public int inEMPNO { get; set; }
    [NotMapped]
    public int inDG { get; set; }
    [NotMapped]
    public int inDEPT { get; set; }
    [NotMapped]
    public int inDESGN { get; set; }

    [NotMapped]
    public int? Outl_id { get; set; }
    
    [StringLength(200)]
    [NotMapped]
    public string? Outl_name { get; set; }
}

这里是调用存储过程的函数:

public virtual string  GetManagerList(int? empId, int? dg, int? dept, int? desgnation)
{
        try
        {
            using (var ctx = new hrmsContext())
            using (var cmd = ctx.Database.GetDbConnection().CreateCommand())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "MANGERLIST";

                var EMPNOParam = new OracleParameter("EMP_NO", OracleDbType.Int32, empId, ParameterDirection.Input);
                var DGParam = new OracleParameter("inDG", OracleDbType.Int32, dg, ParameterDirection.Input);
                var DEPTParam = new OracleParameter("inDEPT", OracleDbType.Int32, dept, ParameterDirection.Input);
                var DESGNParam = new OracleParameter("inDESGN", OracleDbType.Int32, desgnation, ParameterDirection.Input);
                var l_idParam = new OracleParameter("Outl_id", OracleDbType.RefCursor, ParameterDirection.Output);

                cmd.Parameters.AddRange(new[] { EMPNOParam, DGParam, DEPTParam, DESGNParam, l_idParam });

                cmd.Connection.Open();
                var result = cmd.ExecuteNonQuery();
                cmd.Connection.Close();

                var assetRegistered = l_idParam.Value;
                return assetRegistered.ToString();
            }
        }
        catch (Exception x)
        {
            return null;
        }
}
oracle asp.net-core entity-framework-core
1个回答
0
投票

您的程序无效,因为您需要将变量作为绑定参数传递(而不是尝试将它们嵌入字符串中,这将编译但在运行时失败):

create or replace PROCEDURE MANGERLIST
(
  EMPNO IN  NUMBER 
, DG    IN  NUMBER 
, DEPT  IN  NUMBER 
, DESGN IN  NUMBER 
,l_name OUT sys_refcursor
)
AS 
  l_sql VARCHAR2(800);
BEGIN
  l_sql := 'select TRIM(emp_no) , TRIM(EMP_NAME_E) FROM hr."VempDtls" where DESG_TYPE <> 99';

  IF DESGN in (4,5,6,99) then
    IF EMPNO = 213 then
      l_sql := l_sql || ' and emp_no =85';    
    ELSif EMPNO in (341,215,278) then
      l_sql := l_sql || ' and emp_no in(213,215,219,58)';
    ELSif EMPNO in (336,323) then
      l_sql := l_sql || ' and emp_no in(277,219,58)';
    ELSE  
      l_sql := l_sql || ' and dg_code = :1'
                     || ' and emp_no <> :2'
                     || ' and (  DESG_TYPE in (2,3,0,1)'
                     ||      'OR (   dept_code = :3'
                     ||         'and DESG_TYPE in (4,5,6)'
                     ||         'and DESG_TYPE < :4'
                     ||         ')'
                     ||       ')';
      OPEN l_name FOR l_sql USING dg, empno, dept, desgn;
      RETURN;
    END IF;
  ELSif DESGN in (2,3) and EMPNO != 58 then 
    l_sql := l_sql || ' and emp_no=120';
  END IF;

  OPEN  l_name FOR l_sql;
END MANGERLIST;
/
© www.soinside.com 2019 - 2024. All rights reserved.