簡體   English   中英

如何從asp.net調用pl / sql函數?

[英]how to call pl/sql function from asp.net?

我在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驅動程序將其稱為存儲過程即可

這樣的事情可以讓你走上正軌:

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頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM