问题
C#:
public DataSet ListaClientes()
{
DataSet ds = new DataSet();
try
{
System.Data.OracleClient.OracleConnection conexion = Conexion.GetConnection();
if (conexion.State == System.Data.ConnectionState.Open)
{
System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand();
cmd.Connection = conexion;
cmd.CommandText = "ListadoClientes";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("resul", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
conexion.Close();
System.Data.OracleClient.OracleDataAdapter da = new System.Data.OracleClient.OracleDataAdapter(cmd);
da.Fill(ds);
}
return ds;
}
catch(Exception e)
{
throw e;
}
}
Oracle stored procedure:
CREATE OR REPLACE PROCEDURE ListadoClientes(resul OUT sys_refcursor)
IS
BEGIN
OPEN resul for select ID ,NOMBRES ,APELLIDOS ,CEDULA ,DIRECCION ,TELEFONO
from cliente;
END ListadoClientes;
ERROR visible from the C# CATCH block:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'LISTADOCLIENTES'
ORA-06550: line 1, column 7: PL/SQL: Statement ignored
回答1:
Is there a reason why you do not use a function instead of procedure?
CREATE OR REPLACE FUNCTION ListadoClientes() RETURN sys_refcursor
IS
resul Sys_refcursor;
BEGIN
OPEN resul for select ID ,NOMBRES ,APELLIDOS ,CEDULA ,DIRECCION ,TELEFONO
from cliente;
RETURN resul;
END ListadoClientes;
Then in C# you must change it to this:
cmd.Parameters.Add("resul", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
When you run da.Fill(ds);
then the function is executed, i.e. using cmd.ExecuteNonQuery();
executes the function twice.
Anyway, for a procedure the right way should be this one:
cmd.CommandText = "ListadoClientes(:resul)";
回答2:
Finally, I managed to make it worked this way:
stored procedure
create or replace PROCEDURE ListadoClientes(resul OUT sys_refcursor) IS BEGIN OPEN resul for select ID ,NOMBRES ,APELLIDOS ,CEDULA ,DIRECCION ,TELEFONO from cliente; END ListadoClientes;
source c#
reference using Oracle.DataAccess.Client;
public DataSet ListaClientes()
{
DataSet ds = new DataSet();
try
{
OracleConnection conexion = Conexion.GetConnection2();
if (conexion.State == System.Data.ConnectionState.Open)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conexion;
cmd.CommandText = "ListadoClientes";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("resul", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
conexion.Close();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds);
}
return ds;
}
catch(Exception e)
{
throw e;
}
}
来源:https://stackoverflow.com/questions/32953374/return-sys-refcursor-of-the-oracle-to-c-sharp