我在尝试在 C# 中调用我的 DBA 函数时遇到了困难。它在 Oracle 查询浏览器中运行良好,但是当我尝试在 C# 中执行它时失败。
最常见的错误是:“ORA-06502: PL/SQL: numeric or value error: string buffer too small ORA-06512: at line 1”
我试图增加 C# 参数的大小,但没有成功。不知道我做错了什么。
C#代码:
string returnValue = string.Empty;
using (OracleConnection cn = new OracleConnection("ConnectionString"))
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = cn;
cmd.CommandText = "package.function";
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter param1 = new OracleParameter();
OracleParameter param2 = new OracleParameter();
OracleParameter param3 = new OracleParameter();
OracleParameter param4 = new OracleParameter();
OracleParameter param5 = new OracleParameter();
param1.OracleDbType = OracleDbType.Varchar2;
param1.Direction = ParameterDirection.Input;
param1.Value = "Test808";
param1.ParameterName = "var1";
//param1.Size = 2000;
param2.OracleDbType = OracleDbType.Varchar2;
param2.Direction = ParameterDirection.Input;
param2.Value = "68B54814";
param2.ParameterName = "var2";
//param2.Size = 2000;
param3.OracleDbType = OracleDbType.Varchar2;
param3.Direction = ParameterDirection.Input;
param3.Value = "71839";
param3.ParameterName = "var4";
//param3.Size = 2000;
param4.OracleDbType = OracleDbType.Varchar2;
param4.Direction = ParameterDirection.Input;
param4.Value = 55 ;
param4.ParameterName = "var4";
//param4.Size = 2000;
param5.OracleDbType = OracleDbType.Varchar2;
param5.Direction = ParameterDirection.ReturnValue;
param5.ParameterName = "return";
//param5.Size = 2000;
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
cmd.Parameters.Add(param3);
cmd.Parameters.Add(param4);
cmd.Parameters.Add(param5);
try
{
cn.Open();
cmd.ExecuteNonQuery();
returnValue = cmd.Parameters["return"].Value.ToString();
}
catch
{
}
finally
{
cn.Close();
}
}
}
return returnValue;
函数头:
FUNCTION create_rec
(p_mims_pallet_id varchar2
,p_item_no varchar2
,p_packer_id varchar2
,p_tare_wt varchar2
)
return varchar2
第一次尝试 Oracle 调用。从来没有遇到过 SQL 的问题。parameter.Size 被注释掉,因为它似乎没有做任何事情。
能够通过删除各个参数并通过 Parameter.Add 方法创建它们来使其工作。
工作代码:
string returnValue = string.Empty;
using (OracleConnection cn = new OracleConnection("ConString"))
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = cn;
cmd.CommandText = "package.function";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("rv", OracleDbType.Varchar2, 200, "", ParameterDirection.ReturnValue);
cmd.Parameters.Add("var1", OracleDbType.Varchar2, 20, "Test808", ParameterDirection.Input);
cmd.Parameters.Add("var2", OracleDbType.Varchar2, 20, "68B54814", ParameterDirection.Input);
cmd.Parameters.Add("var3", OracleDbType.Varchar2, 20, "71839", ParameterDirection.Input);
cmd.Parameters.Add("var4", OracleDbType.Decimal, 55, ParameterDirection.Input);
try
{
cn.Open();
cmd.ExecuteNonQuery();
returnValue = cmd.Parameters["rv"].Value.ToString();
}
catch
{
}
finally
{
cn.Close();
}
}
}
return returnValue;