时间:2021-07-01 10:21:17 帮助过:5人阅读
3.c#调用存储过程
/// <summary> /// 返回结果集 /// </summary> /// <param name="name">存储过程传入参数,用于筛选数据</param> /// <param name="count">存储过程返回参数,返回筛选出来的数据总条数</param> /// <returns></returns> public DataSet RunProcedure(string name,out int count) { count = 0;
string connectionString = "Data Source=orcl;User ID=用户名;Password=口令;Unicode=True"; using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); OracleDataAdapter sqlDA = new OracleDataAdapter("Sp_Students", connection); OracleParameter[] paras ={ new OracleParameter("p_name",name), new OracleParameter("p_totalRecords",count), new OracleParameter("v_cur", OracleType.Cursor) //返回的游标 }; paras[1].Direction = ParameterDirection.Output; paras[2].Direction = ParameterDirection.Output; sqlDA.SelectCommand.Parameters.AddRange(paras); sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;//设置 使用存储过程 sqlDA.Fill(dataSet); connection.Close(); count = Convert.ToInt32(paras[1].Value); return dataSet; } }
4.测试存储过程
4.1 创建表:
-- Create table create table TB_STUDENTS ( ID NVARCHAR2(32) default sys_guid() not null, NAME NVARCHAR2(32), AGE NUMBER ); commit; --插入测试数据 insert into TB_STUDENTS (ID, NAME, AGE) values (‘DE3A7D4D5D054992B40761BFA2391F5‘, ‘张三‘, 20); insert into TB_STUDENTS (ID, NAME, AGE) values (‘137ACF3D6C0A4BE0B71423A1F7D8452‘, ‘李四‘, 22); insert into TB_STUDENTS (ID, NAME, AGE) values (‘2AF70A53BCD348EE9E8FF03916D018F‘, ‘王五‘, 18); insert into TB_STUDENTS (ID, NAME, AGE) values (‘2877652AE0A3408A981D8980AE833CF‘, ‘100‘, 55); insert into TB_STUDENTS (ID, NAME, AGE) values (‘7B356A73B80C4ED0BFAFA1DD96256E0‘, ‘100‘, 12); insert into TB_STUDENTS (ID, NAME, AGE) values (‘1FF0A99B47FF4056BBDA7E3B4262C61‘, ‘100‘, 26); commit;
4.2 c#调用:
DataTable dt = null; int count ; DataSet ds = RunProcedure("100",out count); if (ds != null && ds.Tables.Count > 0) { dt = ds.Tables[0]; }
Oracle存储过程返回结果集
标签: