使用c#中的自定义类型返回值调用oracle函数

时间:2016-12-02 14:10:45

标签: c# oracle pipeline

我有自定义oracle数据类型:

type tClients is record(
    id        NUMBER,
    name    VARCHAR2(1000 CHAR),
    tax    varchar2(30),
    IsClient      number
);

type tTClients is table of tClients;

和oracle函数,tTClients的返回值为pipelined:

function getClients(p_Organization_Name  varchar2,
                          p_Director_Last_Name varchar2,
                         ) return tTClients
        pipelined;

我使用ODP.NET并在C#中尝试下一个代码:

using(OracleConnection conOra=new OracleConnection(oraConStr))
            {
                conOra.Open();
                using (OracleCommand cmd = new OracleCommand("MQ_CRM.CC_NEW.getClients",conOra))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new OracleParameter("p_Organization_Name", OracleDbType.Varchar2, "мяскин", ParameterDirection.Input));
                    cmd.Parameters.Add(new OracleParameter("p_Director_Last_Name", OracleDbType.Varchar2,"", ParameterDirection.Input));
                 //return parameter
                    OracleParameter p = new OracleParameter();
                    p.OracleDbType = OracleDbType.RefCursor;
                    p.UdtTypeName = "MQ_CRM.CC_NEW.tTClients";
                    p.Direction = ParameterDirection.ReturnValue;

                    cmd.Parameters.Add(p);


                    try
                    {
                        cmd.ExecuteReader();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }
            }

有例外ORA-06550。直接执行此函数sqlplus工作正常。 我该如何解决这个问题&

1 个答案:

答案 0 :(得分:1)

您无法直接在ODP.NET中使用自定义类型。但是,您可以将其用作

OracleCommand cmd = new OracleCommand();
cmd.Connection = conOra;
cmd.CommandText = "SELECT * FROM TABLE(MQ_CRM.CC_NEW.getClients(:p_Organization_Name, :p_Director_Last_Name)) ";
cmd.CommandType = CommandType.Text; 
cmd.Parameters.Add(new OracleParameter("p_Organization_Name", OracleDbType.Varchar2, "мяскин", ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter("p_Director_Last_Name", OracleDbType.Varchar2,"", ParameterDirection.Input));
OracleDataReader dr = cmd.ExecuteReader();

如果您想返回OracleDbType.RefCursor,那么您的函数必须返回SYS_REFCURSOR类型,请参阅Using PL/SQL Stored Procedures and REF CURSORsCursor Variable Declaratio n