Using ouput parameter of type SYS_refcursor

喜你入骨 提交于 2019-12-24 11:13:34

问题


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:

 .............
 OracleConnection 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?

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/7738787/using-ouput-parameter-of-type-sys-refcursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!