开发者

Using ouput parameter of type SYS_refcursor

开发者 https://www.devze.com 2023-04-12 23:44 出处:网络
In my database I have a stored procedure with an OUTPUT parameter of type SYS_REFCURSOR. The application side is wrtitten in C#. Can I assign this procedure\'s output parameter toa Datatable like:

In my database I have a stored procedure with an OUTPUT parameter of type SYS_REFCURSOR. The application side is wrtitten in C#. Can I assign this procedure's output parameter to a Datatable like:

 .............
 Or开发者_开发问答acleConnection con=new OracleConnection(......);
 OracleCommand cmd=new OracleCommand("MyStoredProc",con);
 cmd.CommandType=CommandType.StoredProcedure;
 cmd.Parameters.Add("REC_CUR",OracleType.Cursor).Direction=ParameterDirection.Output;
 con.Open();
 cmd.ExecuteNonQuery();
 DataTable dt=(DataTable)cmd.Parameters["REC_CUR"].value;//is this legal?


Here's the answer to my own question. If the output parametr of a stored procedure is of type SYS_REFCURSOR then the command

cmd.Parameters["REC_CUR"].value

will return an OracleDataReader object, not a table. And there's no implicit , nor explicit cast from OracledataReader to DataTable.


You can use OracleDataAdapter like below,

sample code with multiple out parameters with RefCursor :

using (OracleCommand cmd = new OracleCommand("SP1", OraCon) { CommandType = System.Data.CommandType.StoredProcedure })
{
    var parm_nic = cmd.Parameters.Add("parm_nic", OracleDbType.NVarchar2);
    parm_nic.Value = msgBody;

    var pram_Name = cmd.Parameters.Add("pram_Name", OracleDbType.NVarchar2, 150, ParameterDirection.Output);
    var pram_PAdress = cmd.Parameters.Add("pram_PAdress", OracleDbType.NVarchar2, 150, ParameterDirection.Output);
    var output = cmd.Parameters.Add("pram_status", OracleDbType.RefCursor, ParameterDirection.Output);
    OraCon.Open();

    OracleDataAdapter ad = new OracleDataAdapter(cmd);
    OracleCommandBuilder cb = new OracleCommandBuilder(ad);

    DataTable dt = new DataTable();
    ad.Fill(dt);

    var address = pram_PAdress.Value;
    var name = pram_Name.Value;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消