如何从asp.net调用pl/sql函数?

如何从asp.net调用pl/sql函数?,asp.net,plsql,Asp.net,Plsql,我在oracle中创建了一个函数,如下所示: 我需要从我的asp.net页面调用它。。。 我怎样才能做到 create or replace function fun_GeneratePaper(strDisciplineId IN CHAR, iNoOfQuestions IN integer) return sys_refcursor as r1 sys_refcursor; begin open r1 for select getguid() tmp, Questi

我在oracle中创建了一个函数,如下所示:

我需要从我的asp.net页面调用它。。。 我怎样才能做到

 create or replace function fun_GeneratePaper(strDisciplineId IN CHAR,
  iNoOfQuestions IN integer)
  return sys_refcursor as
  r1 sys_refcursor;
  begin open r1 for select getguid() tmp,
   QuestionNo,QuestionText,
   Option1,Option2,
   Option3,Option4,
   Correctanswer,Disciplineid
   from tbliffcoQuestionmaster
   where DisciplineId=strDisciplineId
   AND  rownum <= iNoOfQuestions ;
  return(r1);
  end;

使用Oracle.net驱动程序像存储过程一样调用它

使用Oracle.net驱动程序像存储过程一样调用它

OracleConnection connection = null;
OracleCommand command = null;
OracleDataReader reader = null;
OracleTransaction transaction = null; 

try
{
    connection = new OracleConnection(connectionString);;
    command = new OracleCommand();
    command.Connection = connection;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "fun_GeneratePaper"; // May need to qualify with SCHEMA.fun_GeneratePaper

    command.Parameters.Add("strDisciplineId", OracleType.Char);
    command.Parameters["strDisciplineId"].Value = disciplineId;

    command.Parameters.Add("iNoOfQuestions", OracleType.Int32);
    command.Parameters["iNoOfQuestions"].Value = numberOfQuestions; 

    command.Parameters.Add("sys_refcursor", OracleType.Cursor);
    command.Parameters["sys_refcursor"].Direction = ParameterDirection.Output;

    connection.Open();
    transaction = connection.BeginTransaction();
    command.Transaction = transaction;
    reader = command.ExecuteReader();

    while(reader.Read())
    {
        // Do work
    }

    transaction.Commit();

}
catch(Exception e)
{
    if (transaction != null)
    {
        transaction.Rollback();
    }

    // Handle it
}
finally
{
    if (connection != null)
    {
        connection.Close();
    }
}

注意:这类代码可能不属于您的ASP.NET页面,不管怎样。

类似这样的代码应该会让您走上正确的轨道:

OracleConnection connection = null;
OracleCommand command = null;
OracleDataReader reader = null;
OracleTransaction transaction = null; 

try
{
    connection = new OracleConnection(connectionString);;
    command = new OracleCommand();
    command.Connection = connection;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "fun_GeneratePaper"; // May need to qualify with SCHEMA.fun_GeneratePaper

    command.Parameters.Add("strDisciplineId", OracleType.Char);
    command.Parameters["strDisciplineId"].Value = disciplineId;

    command.Parameters.Add("iNoOfQuestions", OracleType.Int32);
    command.Parameters["iNoOfQuestions"].Value = numberOfQuestions; 

    command.Parameters.Add("sys_refcursor", OracleType.Cursor);
    command.Parameters["sys_refcursor"].Direction = ParameterDirection.Output;

    connection.Open();
    transaction = connection.BeginTransaction();
    command.Transaction = transaction;
    reader = command.ExecuteReader();

    while(reader.Read())
    {
        // Do work
    }

    transaction.Commit();

}
catch(Exception e)
{
    if (transaction != null)
    {
        transaction.Rollback();
    }

    // Handle it
}
finally
{
    if (connection != null)
    {
        connection.Close();
    }
}
注意:无论如何,这种代码可能都不属于您的ASP.NET页面