问题
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