【问题标题】:Issue in fetching data from SP (Oracle Database)从 SP(Oracle 数据库)获取数据的问题
【发布时间】:2014-11-03 17:01:39
【问题描述】:

由于我是新手,我在执行 oracle DB 中的存储过程时遇到了一些问题。这里是 SP,它给出记录作为 %rowtype 类型的输出参数和 l_serno 作为数字类型的输入参数。

Create OR Replace procedure get_product(l_serno in product.serno%type,record out product%rowtype)
is

begin

select * into record from product where serno=l_serno;

end get_product;

使用 C#,我正在尝试从 SP 获取数据并将其显示在 gridview 上。

OracleCommand cmd = new OracleCommand("get_product", Conn);
                cmd.CommandType = CommandType.StoredProcedure;
                Conn.Open();
                OracleParameter input = cmd.Parameters.Add("V_SERNO", OracleType.Number);
                OracleParameter output = cmd.Parameters.Add("ITEMS_CURSOR", OracleType.Cursor);
                input.Direction = ParameterDirection.Input;
                output.Direction = ParameterDirection.ReturnValue;
                input.Value = 2;
                OracleDataReader rd = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(rd);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Conn.Close();

这里我收到错误

ORA-06550:第 1 行,第 24 列:

PLS-00306:调用“GET_PRODUCT”时参数的数量或类型错误

ORA-06550:第 1 行,第 7 列:

请让我知道我在这里做错了什么。 提前致谢。

【问题讨论】:

  • 不是ParameterDirection.ReturnValue;,你试过ParameterDirection.Output;吗?;
  • 您好,Hassan Nisar,感谢您的回复。我也尝试使用 ParameterDirection.Output。仍然出现同样的错误。
  • 交叉检查你在参数中提到的OracleType。还要检查这个answer
  • 您好,感谢您的回复。正如你所建议的,我将“product.serno%type 中的 l_serno”更改为“l_serno number”,这起到了作用并开始工作。

标签: c# asp.net oracle stored-procedures


【解决方案1】:

你的程序有这个签名:

(l_serno in product.serno%type,record out product%rowtype)

但是在你的 C# 代码中你指定了这个:

 OracleParameter output = cmd.Parameters.Add("ITEMS_CURSOR", OracleType.Cursor);

游标是指向结果集的指针,不同于变量。您可以更改您的 C# 代码:定义一个属性与 PRODUCT 表的投影匹配的类。或者,更改存储过程以使用引用游标。

第二种方法可能工作量更少(尤其是因为您可以让我们为您做这件事)

create or replace procedure get_product
       (l_serno in product.serno%type,
            record out sys_refcursor)
is
begin
     open record for  
            select * from product
             where serno=l_serno; 
end get_product;

【讨论】:

  • 您好 APC,感谢您的回复。正如您所建议的,我尝试将存储过程输出参数更改为游标。我遇到了同样的错误。然后我将“l_serno in product.serno%type”更改为“l_serno number”,这起到了作用并开始工作
猜你喜欢
  • 2011-11-16
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2011-07-15
相关资源
最近更新 更多