当前位置: 首页 > news >正文

CSharp: Oracle Stored Procedure query table

存储过程查询postgreSQL,Oracle 和sql server,Mysql 有区别。程序调用也是有区别。

oracle sql script:

CREATE OR REPLACE PROCEDURE procSelectSchool(paramSchoolId IN char,p_cursor OUT SYS_REFCURSOR
) AS
BEGINOPEN p_cursor FORSELECT *FROM SchoolWHERE SchoolId = paramSchoolId;
END procSelectSchool;
/-- 查询所有
CREATE OR REPLACE PROCEDURE SelectSchoolAll(p_cursor OUT SYS_REFCURSOR
) AS
BEGINOPEN p_cursor FORSELECT *FROM School;
END SelectSchoolAll;
/
        /// <summary>/// 查询存储过程/// 20241225/// </summary>/// <param name="sql">存储过程名称</param>/// <param name="cmdType"></param>/// <param name="pCursor">游标</param>/// <param name="sqlParams"></param>/// <returns></returns>public static OracleDataReader GetReaderCursor(string sql, CommandType cmdType, OracleParameter pCursor, params OracleParameter[] sqlParams){OracleCommand cmd = new OracleCommand();cmd.CommandType = cmdType;cmd.CommandText = sql;cmd.CommandTimeout = 1000;//if (sqlParams != null)cmd.Parameters.AddRange(sqlParams);cmd.Parameters.Add(pCursor);OracleConnection conn = GetConnection(true);cmd.Connection = conn;cmd.ExecuteNonQuery();return ((OracleRefCursor)pCursor.Value).GetDataReader();}/// <summary>/// 查询存储过程/// </summary>/// <param name="sql">存储过程名称</param>/// <param name="cmdType"></param>/// <param name="pCursor">游标</param>/// <param name="sqlParams"></param>/// <returns></returns>public static DataTable GetDataTableCursor(string sql, CommandType cmdType, OracleParameter pCursor, params OracleParameter[] sqlParams){DataTable dt = new DataTable();OracleCommand cmd = new OracleCommand();cmd.CommandType = cmdType;cmd.CommandText = sql;cmd.CommandTimeout = 1000;//if (sqlParams != null)cmd.Parameters.AddRange(sqlParams);cmd.Parameters.Add(pCursor);OracleConnection conn = GetConnection(true);cmd.Connection = conn;// 使用OracleDataAdapter来填充DataSetusing (OracleDataAdapter adapter = new OracleDataAdapter(cmd)){DataSet dataSet = new DataSet();// 你可以指定一个表名,也可以不指定,让系统自动生成一个表名adapter.Fill(dataSet, "ds");dt = dataSet.Tables[0];}return dt;}

DAL:

       ///<summary>///存储过程  查询记录///https://docs.oracle.com/en/database/oracle/oracle-data-access-components/19.3.2/odpnt/extenBoth.html///https://github.com/oracle/dotnet-db-samples////</summary>///<param name="schoolId">输入参数:SchoolId</param>///<returns>返回SchoolInfo</returns>public SchoolInfo SelectSchool(string schoolId){SchoolInfo school = null;try{//添加输入参数OracleParameter par =new OracleParameter("paramSchoolId", OracleDbType.NChar); // 参数和函数名,都要用小写  OracleDbType.NCharpar.Value=schoolId;// 添加输出参数(REF CURSOR)OracleParameter p_cursor = new OracleParameter{ParameterName = "p_cursor",OracleDbType = OracleDbType.RefCursor,Direction = ParameterDirection.Output};              using (OracleDataReader reader = OracleHelper.GetReaderCursor(databaserole + "procSelectSchool", CommandType.StoredProcedure, p_cursor,par)){if (reader.Read()){school = new SchoolInfo();school.SchoolId =(!DBNull.Equals(reader["SchoolId"],null))? (string) reader["SchoolId"].ToString():"";school.SchoolName =(!DBNull.Equals(reader["SchoolName"],null))? (string) reader["SchoolName"].ToString():"";school.SchoolTelNo =(!DBNull.Equals(reader["SchoolTelNo"],null))? (string) reader["SchoolTelNo"].ToString():"";}}}catch (OracleException ex){throw ex;}return school;}///<summary>///存储过程  查询所有记录///</summary>///<param name="schoolId">无输入参数</param>///<returns>返回表所有记录(List)SchoolInfo</returns>public List<SchoolInfo> SelectSchoolAll(){List<SchoolInfo> list = new List<SchoolInfo>();SchoolInfo school = null;try{// 添加输出参数(REF CURSOR)OracleParameter p_cursor = new OracleParameter{ParameterName = "p_cursor",OracleDbType = OracleDbType.RefCursor,Direction = ParameterDirection.Output};using (OracleDataReader reader = OracleHelper.GetReaderCursor(databaserole + "procSelectSchoolAll", CommandType.StoredProcedure, p_cursor, null)){while (reader.Read()){school = new SchoolInfo();school.SchoolId =(!DBNull.Equals(reader["SchoolId"],null))? (string) reader["SchoolId"].ToString():"";school.SchoolName =(!DBNull.Equals(reader["SchoolName"],null))? (string) reader["SchoolName"].ToString():"";school.SchoolTelNo =(!DBNull.Equals(reader["SchoolTelNo"],null))? (string) reader["SchoolTelNo"].ToString():"";list.Add(school);}}}catch (OracleException ex){throw ex;}return list;}///<summary>///存储过程  查询所有记录///</summary>///<param name="schoolId">无输入参数</param>///<returns>返回(DataTable)School表所有记录</returns>public DataTable SelectSchoolDataTableAll(){DataTable dt = new DataTable();try{// 添加输出参数(REF CURSOR)OracleParameter p_cursor = new OracleParameter{ParameterName = "p_cursor",OracleDbType = OracleDbType.RefCursor,Direction = ParameterDirection.Output};using (DataTable reader = OracleHelper.GetDataTableCursor(databaserole + "SelectSchoolAll", CommandType.StoredProcedure, p_cursor, null)){dt = reader;}}catch (OracleException ex){throw ex;}return dt;}

调用:

    /// <summary>/// /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Form1_Load(object sender, EventArgs e){try{SchoolBLL bLL = new SchoolBLL();this.dataGridView1.DataSource = bLL.SelectSchoolDataTableAll();SchoolInfo info = bLL.SelectSchool("U0002");if (info != null){this.txtId.Text = info.SchoolId;this.txtName.Text = info.SchoolName;this.txtTel.Text = info.SchoolTelNo;}}catch (Exception ex){ex.Message.ToString();}}

输出:


http://www.mrgr.cn/news/81524.html

相关文章:

  • Mac 查询IP配置,网络代理
  • UGUI简单动画制作
  • VS2022+QT6.7 窗口置灰(遮罩)
  • C/C++应该如何使用NI-488.2库?
  • 高德地图自定义折线矢量图形
  • 【踩坑记录】C编程变量未初始化导致的程序异常
  • 重温设计模式--10、单例模式
  • STM32项目之环境空气质量检测系统软件设计
  • 【Git】-- 版本说明
  • DX12 快速教程(2) —— 渲染天蓝色窗口
  • 笔记本通过HDMI转VGA线连接戴尔显示器,wifi不可用或网速变慢
  • 大数据实验二
  • 鸿蒙之路的坑
  • soular使用教程
  • KylinOS V10 SP3下编译openGauss与dolphin插件
  • 操作系统导论读书笔记
  • 水库大坝三维模型的开发和使用3Dmax篇
  • 基于STM32F103控制L298N驱动两相四线步进电机
  • 数据库管理-第275期 Oracle 23ai:画了两张架构图(20241225)
  • idea配置gitee仓库
  • Flink调优----资源配置调优与状态及Checkpoint调优
  • FFmpeg 的常用API
  • 学习数量关系
  • Docker 部署 plumelog 最新版本 实现日志采集
  • Petalinux使用QSPI FLASH引导启动
  • Unity 实现Canvas显示3D物体