没有从c#中的ORACLE函数获取返回值

时间:2016-03-07 12:11:36

标签: c# oracle10g

以下是从oracle函数获取值的C#代码:

 public Int16 CheckGeneralSetting(string p_SysName, string p_UsrName)
        {
            try
            {
                conopen();
                Int16  retVal = 0;
                using (OracleCommand sc = new OracleCommand("udsp_Check_Gen_Setting", PubFun.ConOrcl))
                {
                    sc.CommandType = CommandType.StoredProcedure;
                    sc.Parameters.Add("p_SysName", OracleDbType.NVarchar2).Value = p_SysName.Trim();
                    sc.Parameters.Add("p_UsrName", OracleDbType.NVarchar2).Value = p_UsrName.Trim();
                    var mgrParam = sc.Parameters.Add("retval", OracleDbType.Int16,10, ParameterDirection.ReturnValue);
                    sc.ExecuteNonQuery();
                    conclose();
                    retVal = Convert.ToInt16(mgrParam.Value);
                }
                return retVal;
            }
            catch { return 0; }
            finally {  }
        }

以下是我的Oracle功能: -

create or replace FUNCTION udsp_Check_GEN_SETTING 
(
  p_SysName XXES_LOGRECORD.SYSNAME%TYPE ,p_UsrName XXES_LOGRECORD.USRNAME%TYPE  
) 
RETURN NUMBER IS 
  retval NUMBER(10);
  v_Cou_ number(10);
  v_Max_ number(10); 
  Begin  
  retval:=0;
  Select COUNT(*) INTO v_Cou_ from XXES_GEN_SETTING where Check_Type='Lic_Active' and IsActive='Y'; 
  if v_Cou_>0 Then 
      Select Check_Value INTO v_Max_ from XXES_GEN_SETTING where Check_Type='Lic_Active' and IsActive='Y';  
      Update XXES_LOGRECORD Set LogoffTime=SYSTIMESTAMP where abs(trunc(sysdate) - TRUNC(MODIFYLOGTIME)) >30; 
      Select count(*) INTO v_Cou_ from XXES_LOGRECORD where SysName=p_SysName and UsrName=p_UsrName and LogoffTime is null; 
      if v_Cou_=0 Then
            Select COUNT(*) INTO v_Cou_ from XXES_LOGRECORD where LogoffTime is null; 
            if v_Cou_<v_Max_ Then 
                Insert into XXES_LOGRECORD (SysName,UsrName,LogTime,ModifyLogTime) values (p_SysName,p_UsrName,SYSTIMESTAMP,SYSTIMESTAMP);  
                retVal:=1;
            Else 
                retVal:=0;
            End if;  
       Else 
            Update XXES_LOGRECORD Set ModifyLogTime=SYSTIMESTAMP where SysName=p_SysName and UsrName=p_UsrName and LogoffTime is null; 
            retVal:=1;
       End if; 
  Else
        Update XXES_LOGRECORD Set LogoffTime=SYSTIMESTAMP where abs(trunc(sysdate) - TRUNC(MODIFYLOGTIME)) >30; 
        Select count(*) INTO v_Cou_ from XXES_LOGRECORD where SysName=p_SysName and UsrName=p_UsrName and LogoffTime is null;  
        if v_Cou_=0 then
          Insert into XXES_LOGRECORD (SysName,UsrName,LogTime,ModifyLogTime) values (p_SysName,p_UsrName,SYSTIMESTAMP,SYSTIMESTAMP); 
        else 
          Update XXES_LOGRECORD Set ModifyLogTime=SYSTIMESTAMP where SysName=p_SysName and UsrName=p_UsrName and LogoffTime is null;
        end if; 
        retval:=1;
  End if;
  return retval;
End;

*

  

现在我的问题是,我无法从我的c#函数中的oracle函数获取返回值。我是   从这个Oracle函数中获取SQL开发人员的输出但不是   来自C#代码。如果我在c#中检查mgrParam.value,则值总是为0但在sql developer中它的1是正确的。转换输出参数时出现错误(附带屏幕截图)enter image description here。有什么帮助吗?

* 以下是我在SQL开发人员中的调用:

set serveroutput on
begin
 dbms_output.put_line ('output' || ' ' || udsp_Check_GEN_SETTING ('prem','prem'));
   end;

错误截图

2 个答案:

答案 0 :(得分:0)

您正在为您的变量分配输入参数,但是当它在Oracle逻辑中填充时,您不会将其从参数集合中提取出来:

sc.ExecuteNonQuery();
var retVal = sc.Parameters["retval"].Value;

另请注意,无论您在C#中的名称是什么,Oracle都希望将返回值作为第一个参数:

using (OracleCommand sc = new OracleCommand("udsp_Check_Gen_Setting", PubFun.ConOrcl))
{
    sc.CommandType = CommandType.StoredProcedure;
    sc.Parameters.Add("retval", OracleDbType.Int16,10, ParameterDirection.ReturnValue);
    sc.Parameters.Add("p_SysName", OracleDbType.NVarchar2).Value = p_SysName.Trim();
    sc.Parameters.Add("p_UsrName", OracleDbType.NVarchar2).Value = p_UsrName.Trim();

    sc.ExecuteNonQuery();
    conclose();
    retVal = Convert.ToInt16(sc.Parameters["retval"].Value);
}
return retVal;

答案 1 :(得分:0)

您应该在关闭连接之前检索该值:

retVal = Convert.ToInt16(mgrParam.Value);
conclose();