调用oracle过程

时间:2017-03-30 20:04:21

标签: c# oracle visual-studio-2013 plsql

我正在调用一个具有OUT参数(CURSOR类型)的过程。我似乎错过了一些东西,因为我得到了错误:"错误的数字或类型的参数......"。我的代码如下:

 public static DataTable SearchClient()
    {
        try
        {
            using (OleDbCommand cmd = new OleDbCommand(packetName + ".select_A1001310"))
            {
                cmd.CommandType = CommandType.StoredProcedure;                                        
                cmd.Parameters.Add("io_cursor", OracleType.Cursor).Direction = ParameterDirection.Output;


                SqlManager sqlManager = new SqlManager();
                return sqlManager.GetDataTable(cmd);
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
            System.Console.WriteLine(ex);
            //TODO; Handle exception
        }
        return null;
    }

PROCEDURE:

TYPE lcursor_data IS REF CURSOR;        
PROCEDURE select_A1001310(io_cursor OUT lcursor_data)
      AS
      BEGIN
        OPEN io_cursor FOR
        --
          SELECT client_id
            FROM a1001310
           WHERE status = 'A';
        --
      EXCEPTION
        WHEN OTHERS THEN
          IF io_cursor%ISOPEN THEN
            CLOSE io_cursor;
          END IF;
        --REVIRE: EXCEPTION HANDLER
      END select_A1001310;

有什么想法吗?

更新 如果我更改我的代码,使用OracleClient而不是OleDb,它可以工作。有没有办法使用oleDb而不是OracleClient包含Cursor Type参数? OracleClient代码方法如下

public static DataTable SearchClient()
{
    string connection = ConfigurationManager.ConnectionStrings["DBConnection_Oracle"].ToString();
    string procedure = packetName + ".p_search_client";

    OracleParameter[] parameters = new OracleParameter[1];
    parameters[0] = new OracleParameter("io_cursor", OracleType.Cursor, 4000, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Current, String.Empty);

    DataTable dt = new DataTable();
    dt = DataManager_Oracle.GetDataTable_(connection, procedure, parameters);
    return dt;
}

1 个答案:

答案 0 :(得分:1)

如果存储过程返回行集,则必须在web.config中将PLSQLRSet设置为TRUE

<connectionStrings> 
        <add name="DBConnection" connectionString="Provider=MSDAORA.1;Data Source=DB;User Id=user;Password=pass;enlist=false;PLSQLRSet=true;OLE DB Services = -4" providerName="Oracle.DataAccess"/> 
</connectionStrings>