我正在尝试使用C#执行Oracle存储过程(1个输入和2个输出参数).
我的表包含3列;整数id和2个varchar2类型列.@H_502_3@
这是表定义:@H_502_3@
CREATE TABLE TESTTABLE ( ID INT Not Null,FNAME VARCHAR2(200),LNAME VARCHAR2(200),Constraint PK Primary Key (ID) );
这是我的存储过程:@H_502_3@
create or replace PROCEDURE TESTP ( tempID IN TESTTABLE.ID%Type,tempName Out TESTTABLE.NAME%TYPE,tempLName out TESTTABLE.LNAME%TYPE ) AS BEGIN select Name,LNAME into tempName,tempLName from TestTable where ID = tempID; END;
try { Int32 id = 1; string FName = "",LName = ""; using (_ora.GetOracleConnection()) { Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand("TESTP",_ora.GetOracleConnection()); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("tempId",Oracle.DataAccess.Client.OracleDbType.Int32,ParameterDirection.Input).Value = id; cmd.Parameters.Add("tempName",Oracle.DataAccess.Client.OracleDbType.Varchar2,200,ParameterDirection.Output).Value = FName; cmd.Parameters.Add("tempLName",ParameterDirection.Output).Value = LName; cmd.ExecuteNonQuery(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
Oracle.DataAccess.Client.OracleException ORA-06502: PL/sql: numeric or value error
ORA-06512: at “USMANDBA.TESTP”,line 9@H_502_3@
谁能帮我 ?@H_502_3@
解决方法
代码中的这些修改对我有用:
using (connection) { Int32 id = 1; OracleCommand cmd = new OracleCommand("TESTP",connection); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("tempID",OracleDbType.Int32,ParameterDirection.Input).Value = id; cmd.Parameters.Add("tempName",OracleDbType.Varchar2,200).Direction = ParameterDirection.Output; cmd.Parameters.Add("tempLName",200).Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); string FName = cmd.Parameters["tempName"].Value.ToString(); string LName = cmd.Parameters["tempLName"].Value.ToString(); }
您还可以在Oracle过程中添加异常块来处理no_data_found异常并避免ORA-01403错误,如下所示:@H_502_3@
CREATE OR REPLACE PROCEDURE TESTP (tempID IN TESTTABLE.ID%Type,tempName out TESTTABLE.NAME%TYPE,tempLName out TESTTABLE.LNAME%TYPE) AS BEGIN select Name,LNAME Into tempName,tempLName from TestTable Where ID = tempID; EXCEPTION WHEN NO_DATA_FOUND THEN tempName := null; tempLName := null; END;