我是Oracle新手。当我尝试在以下过程中获取out
参数的值时,我收到异常:
该程序的工作是提供有关药物的一些信息,并给出其ID,并将结果存储在out
参数中
create or replace
PROCEDURE RETREIVE_DRUG(
drugId in "Drug"."DRUG_ID"%type,
drugName OUT "Drug"."NAME"%type,
prodDate OUT "Drug"."PRODUCTION_DATE"%type,
expireDate OUT "Drug"."EXPIRATION_DATE"%type,
price OUT "Drug"."PRICE"%type,
description OUT "Drug"."DESCRIPTION"%type,
quantity OUT "Drug"."QUANTITY"%type,
buying OUT "Drug"."COST"%type,
company OUT "Drug"."COMPANY"%type
)
AS
BEGIN
SELECT "NAME", "PRODUCTION_DATE", "EXPIRATION_DATE", "PRICE", "DESCRIPTION", "QUANTITY", "COST", "COMPANY"
INTO drugName, prodDate, expireDate, price, description, quantity, buying, company
FROM "Drug"
WHERE "Drug"."DRUG_ID" = drugId;
END RETREIVE_DRUG;
调用该过程的c#代码如下
public String[] Get_Drug(int id)
{
OpenConnection();
OracleCommand oraCmd = new OracleCommand();
oraCmd.Connection = con;
oraCmd.CommandType = CommandType.StoredProcedure;
oraCmd.CommandText = "RETREIVE_DRUG";
OracleParameter[] param = new OracleParameter[9];
param[0] = new OracleParameter("DRUG_ID", OracleDbType.Decimal);
param[0].Value = id;
param[1] = new OracleParameter("NAME", OracleDbType.Varchar2);
param[1].Direction = ParameterDirection.Output;
param[2] = new OracleParameter("PRODUCTION_DATE", OracleDbType.Date);
param[2].Direction = ParameterDirection.Output;
param[3] = new OracleParameter("EXPIRATION_DATE", OracleDbType.Date);
param[3].Direction = ParameterDirection.Output;
param[4] = new OracleParameter("PRICE", OracleDbType.Decimal);
param[4].Direction = ParameterDirection.Output;
param[5] = new OracleParameter("DESCRIPTION", OracleDbType.Varchar2);
param[5].Direction = ParameterDirection.Output;
param[6] = new OracleParameter("QUANTITY", OracleDbType.Decimal);
param[6].Direction = ParameterDirection.Output;
param[7] = new OracleParameter("COST", OracleDbType.Decimal);
param[7].Direction = ParameterDirection.Output;
param[8] = new OracleParameter("COMPANY", OracleDbType.Varchar2);
param[8].Direction = ParameterDirection.Output;
oraCmd.Parameters.AddRange(param);
oraCmd.ExecuteNonQuery();
CloseConnection();
String[] ret = new String[8];
ret[0] =oraCmd.Parameters["NAME"].Value.ToString();
ret[1] =oraCmd.Parameters["PRODUCTION_DATE"].Value.ToString();
ret[2] =oraCmd.Parameters["EXPIRATION_DATE"].Value.ToString();
ret[3] =oraCmd.Parameters["PRICE"].Value.ToString();
ret[4] =oraCmd.Parameters["DESCRIPTION"].Value.ToString();
ret[5] =oraCmd.Parameters["QUANTITY"].Value.ToString();
ret[6] =oraCmd.Parameters["COST"].Value.ToString();
ret[7] =oraCmd.Parameters["COMPANY"].Value.ToString();
return ret;
}
但是当我运行代码时,会出现以下异常:
PL \ SQL:数字或值错误
虽然我确定名称和类型。请帮忙。我在这个问题上做了很多努力而没有能够解决它!
答案 0 :(得分:0)
我定义了varchar
个参数(drugName
,description
,company
)的大小,并且更改了参数名称,就像它们在Oracle代码中一样,例如,{{ 1}}而不是buying
。最后通过名称和下面的函数添加绑定工作正常:
COST
如果按位置绑定参数,可能第二步和第三步不是键,但按名称定义更具可读性。以下是我用于测试目的的表结构和示例数据:
public String[] Get_Drug(int id)
{
OracleConnection con = new OracleConnection();
con.ConnectionString = "User Id=scott;Password=tiger;Data Source=XE";
con.Open();
OracleCommand oraCmd = new OracleCommand();
oraCmd.Connection = con;
oraCmd.BindByName = true;
oraCmd.CommandType = CommandType.StoredProcedure;
oraCmd.CommandText = "RETREIVE_DRUG";
OracleParameter[] param = new OracleParameter[9];
param[0] = new OracleParameter("drugId", OracleDbType.Decimal);
param[0].Direction = ParameterDirection.Input;
param[0].Value = id;
param[1] = new OracleParameter("drugName", OracleDbType.Varchar2);
param[1].Direction = ParameterDirection.Output;
param[1].Size = 30;
param[2] = new OracleParameter("prodDate", OracleDbType.Date);
param[2].Direction = ParameterDirection.Output;
param[3] = new OracleParameter("expireDate", OracleDbType.Date);
param[3].Direction = ParameterDirection.Output;
param[4] = new OracleParameter("price", OracleDbType.Decimal);
param[4].Direction = ParameterDirection.Output;
param[5] = new OracleParameter("description", OracleDbType.Varchar2);
param[5].Size = 30;
param[5].Direction = ParameterDirection.Output;
param[6] = new OracleParameter("quantity", OracleDbType.Decimal);
param[6].Direction = ParameterDirection.Output;
param[7] = new OracleParameter("buying", OracleDbType.Decimal);
param[7].Direction = ParameterDirection.Output;
param[8] = new OracleParameter("company", OracleDbType.Varchar2);
param[8].Size = 30;
param[8].Direction = ParameterDirection.Output;
oraCmd.Parameters.AddRange(param);
oraCmd.ExecuteNonQuery();
con.Close();
String[] ret = new String[8];
ret[0] = oraCmd.Parameters["drugName"].Value.ToString();
ret[1] = oraCmd.Parameters["prodDate"].Value.ToString();
ret[2] = oraCmd.Parameters["expireDate"].Value.ToString();
ret[3] = oraCmd.Parameters["price"].Value.ToString();
ret[4] = oraCmd.Parameters["description"].Value.ToString();
ret[5] = oraCmd.Parameters["quantity"].Value.ToString();
ret[6] = oraCmd.Parameters["buying"].Value.ToString();
ret[7] = oraCmd.Parameters["company"].Value.ToString();
return ret;
}