如何使用C#使用记录表输出参数调用PL / SQL存储过程?

时间:2018-12-07 15:33:42

标签: c# oracle plsql out-parameters

  

ORA-06550:第1行,第7列:
  PLS-00306:“ GET_STUDENTS”调用中参数的数量或类型错误

     

ORA-06550:第1行,第7列:
  PL / SQL:语句被忽略

我不断收到此错误,但我不知道自己在做什么错。我尝试了不同的方法,但仍然没有运气。

谢谢。

create or replace PACKAGE APPS.XXHP_LT AS
  TYPE student_rec IS RECORD
  (
     first_name varchar2(30)
    ,last_name varchar2(30)
    ,grade number
  );

  TYPE student_tab IS TABLE OF student_rec INDEX BY BINARY_INTEGER;

  PROCEDURE get_students
  (
    p_grade           IN  number
    ,x_students         OUT student_tab
    ,x_highest_grade    OUT NOCOPY NUMBER
  );
end;
/
create or replace PACKAGE BODY APPS.XXHP_LT AS
  PROCEDURE get_students
  (
    p_grade           IN  number
    ,x_students         OUT student_tab
    ,x_highest_grade    OUT NOCOPY NUMBER
  )
  IS
  begin
    x_students(1).first_name := 'Scarlett';
    x_students(1).last_name := 'Johansson';
    x_students(1).grade := 4;

    x_students(2).first_name := 'Jennifer';
    x_students(2).last_name := 'Lawrence';
    x_students(2).grade := 3;

    x_students(3).first_name := 'Megan';
    x_students(3).last_name := 'Fox';
    x_students(3).grade := 2;

    x_students(4).first_name := 'Gal';
    x_students(4).last_name := 'Gadot';
    x_students(4).grade := 1;

    select count(grade) into x_highest_grade from table(x_students) where grade >= p_grade;
  end;
end;
/
DECLARE
  P_GRADE NUMBER;
  X_STUDENTS APPS.XXHP_LT.STUDENT_TAB;
  X_HIGHEST_GRADE NUMBER;
BEGIN
  P_GRADE := 4;

  XXHP_LT.GET_STUDENTS(
    P_GRADE => P_GRADE,
    X_STUDENTS => X_STUDENTS,
    X_HIGHEST_GRADE => X_HIGHEST_GRADE
  );

  DBMS_OUTPUT.PUT_LINE('Your Desired Grade = ' || P_GRADE);
  DBMS_OUTPUT.PUT_LINE('Number of Matches = ' || X_HIGHEST_GRADE);
  DBMS_OUTPUT.PUT_LINE('');

  FOR i IN X_STUDENTS.FIRST .. X_STUDENTS.LAST
  LOOP
    DBMS_OUTPUT.PUT_LINE(X_STUDENTS(i).first_name || ' ' || X_STUDENTS(i).last_name || ' ' || X_STUDENTS(i).grade);
  END LOOP;
END;

此运行的输出如下,这意味着PLSQL代码没有错。

Your Desired Grade = 4
Number of Matches = 1

Scarlett Johansson 4
Jennifer Lawrence 3
Megan Fox 2
Gal Gadot 1

我用下面的代码编写了一个小型控制台应用程序,并且不断出现上述错误。

static void Main(string[] args)
{
    int retValue = 0;
    string ConnectionString = ConfigurationManager.ConnectionStrings["EBS"].ConnectionString;
    string SQL = @"APPS.XXHP_LT.GET_STUDENTS";

    OracleParameter opStudents = new OracleParameter("X_STUDENTS", OracleDbType.Raw, ParameterDirection.Output);
    opStudents.UdtTypeName = "APPS.XXHP_LT.STUDENT_TAB";
    //opEmpOrgsOutSmTab.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
    //opEmpOrgsOutSmTab.Size = 1000;
    //opEmpOrgsOutSmTab.ArrayBindSize = Enumerable.Repeat(10000, opEmpOrgsOutSmTab.Size).ToArray();

    OracleParameter opHighestGrade = new OracleParameter("X_HIGHEST_GRADE", OracleDbType.Int32, ParameterDirection.Output);

    List<OracleParameter> paraList = new List<OracleParameter>();
    paraList.Add(new OracleParameter("P_GRADE", OracleDbType.Int32, 4, ParameterDirection.Input));
    paraList.Add(opStudents);
    paraList.Add(opHighestGrade);

    using (OracleConnection dbConnection = new OracleConnection(ConnectionString))
    {
        try
        {
            dbConnection.Open();

            using (OracleCommand cmd = new OracleCommand(SQL, dbConnection))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.BindByName = true;
                cmd.Parameters.AddRange(paraList.ToArray());

                retValue += cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            retValue = -1;
            Console.WriteLine(ex.Message);
        }
        finally
        {
            dbConnection.Dispose();
        }
    }

    Console.Read();
}

0 个答案:

没有答案