Return sys_refcursor of the oracle to c#

我只是一个虾纸丫 提交于 2019-12-30 13:38:33

问题


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

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