我有這個pl/sql函數,它唯一的作用是驗證用戶是否存在於數據庫中,如果用戶存在,則返回「Y」,但如果用戶不存在這個返回「N」,我想要的是得到我在pl/sql在c#中返回的值。使用OracleCommand執行pl/sql函數
我使用Oracle 10g
CREATE OR REPLACE FUNCTION KRIST.f_Login (userName IN VARCHAR2,
password IN VARCHAR2)
RETURN VARCHAR2
IS
CURSOR USERFINDER IS
SELECT IdEmpleado
FROM EMPLEADO
WHERE Usuario=userName
AND Clave=password;
id number;
returnVal VARCHAR2(1);
BEGIN
OPEN USERFINDER;
FETCH USERFINDER INTO id;
IF(id IS NULL) THEN
returnVal:='Y';
RETURN returnVal;
END IF;
returnVal:='N';
RETURN returnVal;
CLOSE USERFINDER;
END;
/
我怎樣才能實現這一功能,並在變量得到的結果...我有茨艾倫代碼,但不工作
OracleCommand cmd = new OracleCommand("krist.p_login",conn);
cmd.CommandType = CommandType.StoredProcedure; // use StoredProcedure with Functions as well
OracleParameter returnVal = new OracleParameter("returnVal",null);
OracleParameter p_one = new OracleParameter("userName","kristian");
OracleParameter p_two = new OracleParameter("password", "kristian");
returnVal.OracleType = OracleType.VarChar;
returnVal.Size = 1;
p_one.OracleType = OracleType.VarChar;
p_two.OracleType = OracleType.VarChar;
p_one.DbType = DbType.String;
p_two.DbType = DbType.String;
returnVal.DbType = DbType.String;
returnVal.Direction = ParameterDirection.ReturnValue;
p_one.Direction = ParameterDirection.Input;
p_two.Direction = ParameterDirection.Input;
cmd.Parameters.Add(p_one);
cmd.Parameters.Add(p_two);
cmd.Parameters.Add(returnVal);
cmd.ExecuteNonQuery();
String bval = Convert.ToString(returnVal.Value);
return bval;
確實更容易,使用count(*)並查看返回的行是否超過0行 – Andrew
計數更容易,更好。如果userName不存在或者密碼錯誤,則SELECT IdEmpleado將引發NO_DATA_FOUND豁免。 SELECT COUNT(*)返回0 –