Home  >  Article  >  Web Front-end  >  oracle stored procedure in asp.net (picture and text)_jquery

oracle stored procedure in asp.net (picture and text)_jquery

WBOY
WBOYOriginal
2016-05-16 15:45:312148browse

In large database systems, stored procedures and triggers play an important role. Whether it is a stored procedure or a trigger, it is a collection of SQL statements and flow control statements.

ORACLE code

CREATE OR REPLACE PROCEDURE gd_CURSOR(MYCS1 OUT SYS_REFCURSOR,MYCS2 OUT SYS_REFCURSOR,a out varchar)as
BEGIN
 a:='test';
 OPEN MYCS1 FOR
 SELECT 1 from dual;
 OPEN MYCS2 FOR
 SELECT 2 from dual;
END;

C# code

/// <summary>
 /// 执行oracle存储过程返回多个结果集
 /// </summary>
 /// <param name="strProcName">存储过程名称</param>
 /// <param name="ResultCount">返回个数</param>
 /// <param name="paras">参数</param>
 /// <returns>任意对象数组</returns>
 public object[] ExcuteProc_N_Result(string strProcName, int ResultCount, params OracleParameter[] paras)
 {
  using (OracleConnection conn = new OracleConnection("User ID=用户名;Password=密码;Data Source=数据库;"))
  {
  OracleCommand cmd = new OracleCommand(strProcName, conn);
  if (paras != null && paras.Length > 0)
  {
   for (int j = 0; j < paras.Length; j++)
   {
   if (paras[j].Value == null)
   {
    paras[j].Value = DBNull.Value;
   }
   }
  }
  cmd.Parameters.AddRange(paras);
  cmd.CommandType = CommandType.StoredProcedure;
  conn.Open();
  cmd.ExecuteNonQuery();
  int i = 0;
  //int nOutputParametersCount = 0;
  object[] objResult = new object[ResultCount];
  foreach (OracleParameter p in cmd.Parameters)
  {
   if (p.Direction == ParameterDirection.Output || p.Direction == ParameterDirection.InputOutput)
   {
   if (p.Value is OracleDataReader)
   {
    OracleDataReader reader = p.Value as OracleDataReader;
    objResult[i++] = ConvertDataReaderToDataTable(reader);
   }
   else
   {
    objResult[i++] = p.Value;
   }
   }
  }
  return objResult;
  }
 }
 /// <summary> 
 /// 将DataReader 转为 DataTable 
 /// </summary> 
 /// <param name="DataReader">OleDbDataReader</param> 
 protected DataTable ConvertDataReaderToDataTable(OracleDataReader reader)
 {
  DataTable objDataTable = new DataTable("TmpDataTable");
  try
  {
  int intFieldCount = reader.FieldCount;//获取当前行中的列数;
  for (int intCounter = 0; intCounter <= intFieldCount - 1; intCounter++)
  {
   objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
  }
  //populate datatable 
  objDataTable.BeginLoadData();
  //object[] objValues = new object[intFieldCount -1]; 
  object[] objValues = new object[intFieldCount];
  while (reader.Read())
  {
   reader.GetValues(objValues);
   objDataTable.LoadDataRow(objValues, true);
  }
  reader.Close();
  objDataTable.EndLoadData();
  return objDataTable;
  }
  catch (Exception ex)
  {
  throw new Exception("转换出错出错!", ex);
  }
 }

Call method

OracleParameter[] oracleParameter = new OracleParameter[]{
new OracleParameter("MYCS1",OracleType.Cursor),
new OracleParameter("MYCS2",OracleType.Cursor),
new OracleParameter("a",OracleType.VarChar,200),
};
oracleParameter[0].Direction = ParameterDirection.Output;
oracleParameter[1].Direction = ParameterDirection.Output;
oracleParameter[2].Direction = ParameterDirection.Output;

object[] xxx = ExcuteProc_N_Result("gd_CURSOR", 3, oracleParameter);

The above content introduces the oracle stored procedures in asp.net through code.

Next, I will introduce the oracle stored procedure (picture and text) to you through the second method.

Please see the following methods and steps

Step 1: Configure the database that needs to be connected through the Net Manager that comes with ORACLE, such as COST

Step 2: Open the PL/SQL database tool, enter the correct user name and password, and click OK to enter the user who needs to create the stored procedure

Step 3: Understand the format of general stored procedures

create or replace procedure stored procedure name (param1 in type, param2 out type)
as
Variable 1 type (value range);
Variable 2 type (value range);
Begin
Statement block
Exception --Exception handling
When others then
        Rollback;
End;

Step 4: Enter the stored procedure to be created in the SQL input interface

create or replace procedure sp_demo(param1 in varchar2,param2 out varchar2)
/*
* 存储过程实例
*/
as
cnt int;
rst varchar2(100)
Begin
 Select count(*) into cst from Tab_Demo where Col_Value = param1;
 If (cst > 0) then --判断条件
 param2 := '有匹配的值';
 Else
 param2 := '无匹配的值';
 End if;
Exception
 When others then
 Rollback;
End;

As shown below

Step 5: Test the stored procedure just written

exec sp_demo('male');

END

Notes

You cannot delete a stored procedure within another stored procedure, you can only call another stored procedure

If you use create or replace procedure, when creating the stored procedure, be careful not to have the same name as the existing stored procedure under the user, causing the current stored procedure to be overwritten
Stored procedure parameters do not have a value range, in means incoming, out means output

The above introduces Oracle stored procedures in two ways. I hope it will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn