CSharp: Oracle Stored Procedure query table

ops/2024/12/29 10:13:29/

存储过程查询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.ppmy.cn/ops/145896.html

相关文章

计算机网络实验室建设方案

一、计算机网络实验室拓扑结构 计算机网络综合实验室解决方案&#xff0c;是面向高校网络相关专业开展教学实训的综合实训基地解决方案。教学实训系统采用 B&#xff0f;S架构&#xff0c;通过公有云教学实训平台在线学习模式&#xff0c;轻松实现网络系统建设与运维技术的教学…

sql group by 多个字段例子

有表如下&#xff1b; 获取某年份、某地区、某产品的销售总额&#xff0c; 或者根据需要把字段顺序换一下&#xff1b; insert into sales (product, year, region, amount) values (飞机,2000,东部,5); insert into sales (product, year, region, amount) values (飞机,2001,…

spring url匹配

明明是PostMapping却报Request method POST not supported 跟踪源码 HandlerMethod org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch( PartialMatchHelper helper new PartialMatchHelper(infos, request); org.springframew…

使用docker compose安装gitlab

使用docker compose安装gitlab GitLab简介设置GITLAB_HOME路径创建docker挂载目录获取可用的GitLab版本编写docker-compose.yml文件启动docker基础配置 GITLAB_OMNIBUS_CONFIG修改配置 中文设置数据库配置系统邮箱配置 GitLab简介 GitLab是一个基于Git的开源项目&#xff0c…

解决k8s部署dashboard时一直处于Pending状态的问题

直接用离线包就行 命令 [rootk8s-master ~]# docker load -i calico-image-v3.25.0.tar [rootk8s-master ~]# kubectl apply -f calico.yaml链接在https://download.csdn.net/download/weixin_42759398/90192045 [rootk8s-master ~]# docker load -i calico-image-v3.25.0.t…

html5解决华为原生浏览器底部栏兼容问题

前言 我们之前开发的H5网页&#xff0c;有客户反馈页面底部被浏览器下方的工具栏遮住了&#xff0c;经过测试发现目前只有华为原生x5浏览器底部栏出现这个兼容问题&#xff0c;它遮住了底部页面&#xff0c;造成了这个样式兼容问题。 可以看出&#xff1a;在华为浏览器中下方…

数据分析与应用:如何分析7日动销率和滞销率?

目录 0 需求描述 1 数据准备 1.1 订单明细表 1.2 商品信息表 2 SQL实现 3 问题分析与总结 3.1 计算 7 日动销率和滞销率主要有以下方法总结 按商品数量计算 按销售额计算 按销售数量计算 3.1 提高商品 7 日动销率和降低滞销率的方法&#xff1f; 优化商品管理 加强…

【Golang 面试题】每日 3 题(六)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/UWz06 &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 Golang 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏…