将oracle的sys_refcursor返回给c#

时间:2015-10-05 16:27:37

标签: c# oracle stored-procedures oracle11g

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存储过程:

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;

从C#CATCH块可见ERROR:

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

2 个答案:

答案 0 :(得分:1)

您是否有理由不使用函数而不使用过程?

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;

然后在C#中你必须改为:

cmd.Parameters.Add("resul", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

无论如何,对于一个程序,正确的方法应该是这个:

cmd.CommandText = "ListadoClientes(:resul)";

答案 1 :(得分:1)

最后,我设法让它以这种方式运作:

存储过程

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;

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