简体   繁体   中英

Calling stored procedure oracle in c# not all data bound

I need to use an oracle stored procedure in c#. The stored procedure is defined as followsd:

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;
/

In c# I call the procedure:

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;
}

When executing the cmd, i get the following error: RA-01008: not all variables bound

Try this one:

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;

However, I never used PLSQLAssociativeArray as return value, so I don't know whether this code is working.

Maybe return a SYS_REFCURSOR instead of an PLSQLAssociativeArray , this will work for sure.

In this case your C# code may look like this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM