【问题标题】:Calling stored procedure oracle in c# not all data bound在 c# 中调用存储过程 oracle 并非所有数据绑定
【发布时间】:2017-04-28 05:08:40
【问题描述】:

我需要在 c# 中使用 oracle 存储过程。存储过程定义如下:

CREATE OR REPLACE PACKAGE MGRWS10.test_mp
as
type tab_assoc_array is table of varchar2(2000) index by binary_integer;
function f_loten(p_item number) return tab_assoc_array;
procedure p_loten(p_item number, o_result out tab_assoc_array);
end;
/

在c#中我调用了这个过程:

using (var cmd = _connection.CreateCommand()){
    OracleParameter stringArray = new OracleParameter();
    stringArray.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
    stringArray.Direction = ParameterDirection.Output;
    stringArray.ParameterName = "o_result";
    stringArray.Size = 5;
    stringArray.ArrayBindSize = new int[5];
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.BindByName = true;
    cmd.CommandText = "test_mp.p_loten(:p_item, :o_result)";
    cmd.Parameters.Add("p_item", 12942);
    cmd.Parameters.Add(stringArray);
    _connection.Open();

    await cmd.ExecuteNonQueryAsync();
    var x = (string[]) stringArray.Value;
}

执行 cmd 时,出现以下错误: RA-01008:并非所有变量都绑定

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "BEGIN :o_result := test_mp.p_loten(:p_item); END;";
    
    par = cmd.Parameters.Add("o_result", OracleDbType.Varchar2, ParameterDirection.ReturnValue)
    par.CollectionType = OracleCollectionType.PLSQLAssociativeArray
    
    cmd.Parameters.Add("p_item", OracleDbType.Int32, ParameterDirection.Input).Value = 12942;
    

    但是,我从来没有使用PLSQLAssociativeArray作为返回值,所以我不知道这段代码是否有效。

    也许返回 SYS_REFCURSOR 而不是 PLSQLAssociativeArray,这肯定会起作用。

    在这种情况下,您的 C# 代码可能如下所示:

    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "BEGIN :o_result := test_mp.p_loten(:p_item); END;";
    cmd.Parameters.Add("o_result", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
    cmd.Parameters.Add("p_item", OracleDbType.Int32, ParameterDirection.Input).Value = 12942;
    var da = new OracleDataAdapter(cmd); // or cmd.ExecuteNonQueryAsync();
    var dt = new DataTable(); 
    da.Fill(dt);
    

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 2013-01-31
      • 2011-03-23
      • 2011-04-25
      相关资源
      最近更新 更多