时间:2022-05-10 10:13:53 | 栏目:.NET代码 | 点击:次
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。
存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
不多说了,本文通过两种方法介绍asp.net中调用oracle存储过程的方法,具体内容请看下面代码。
调用oracle存储过程方法一:
ORACLE代码
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#代码
/// <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); } }
调用方法
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);
调用oracle存储过程方法二:
存储过程结构如下:
Create or Replace Procedure xx_yy ( i_OrderID in number, i_ReturnValue out number ) is v_RealValue number; v_TotalValue number; v_AdvendorID number; begin 自己写就行 end;
下面讲一下调用:
表结构
create table ORDERTABLE ( ORDERID NUMBER not null, TEXT NUMBER not null )
存储过程
( i_OrderID in number, i_ReturnValue out number ) is spass ordertable.text%type; begin select text into spass from ordertable where orderid=i_OrderID; i_ReturnValue:=spass; exception when no_data_found then i_ReturnValue:=-1; end;
源码:
using System.Data .OracleClient ;//(别忘了添加) OracleConnection Oraclecon = new OracleConnection ("Password=dloco;User ID=dloco;Data Source=dloco;"); OracleCommand myCMD = new OracleCommand(); OracleParameter[] parameters = { new OracleParameter("i_OrderID", OracleType.Number, 10),new OracleParameter("i_ReturnValue",OracleType.Number,10 )}; parameters[0].Value = 1; parameters[1].Direction = ParameterDirection.Output; myCMD.Connection = Oraclecon; myCMD.CommandType = CommandType.StoredProcedure; myCMD.CommandText = "dloco.xx_yy"; myCMD.Parameters .Add (parameters[0]); myCMD.Parameters .Add (parameters[1]); myCMD.Connection.Open(); myCMD.ExecuteNonQuery(); string result=myCMD.Parameters["i_ReturnValue"].Value.ToString(); MessageBox.Show (result); Oraclecon.Close();
以上就是asp.net中调用oracle存储过程的全部内容,希望对大家有所帮助。