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

CSharp: Oracle Stored Procedure query table

oracle sql script:

CREATE OR REPLACE PROCEDURE procSelectSchool(
    paramSchoolId IN char,
    p_cursor OUT SYS_REFCURSOR
) AS
BEGIN
    OPEN p_cursor FOR
        SELECT *
        FROM School
        WHERE SchoolId = paramSchoolId;
END procSelectSchool;
/
 
-- 查询所有
CREATE OR REPLACE PROCEDURE SelectSchoolAll(
    p_cursor OUT SYS_REFCURSOR
) AS
BEGIN
    OPEN p_cursor FOR
        SELECT *
        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来填充DataSet
            using (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.NChar
               par.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.kler.cn/a/452133.html

相关文章:

  • Excel粘贴复制不完整的原因以及解决方法
  • 瑞吉外卖项目学习笔记(九)套餐列表分页查询、新增套餐、图片上传和下载
  • vLLM (2) - 架构总览
  • LeetCode 热题 100_LRU 缓存(35_146_中等_C++)(哈希表 + 双向链表)(构造函数声明+初始化列表=进行变量初始化和赋值)
  • Linux复习4——shell与文本处理
  • 云原生后端开发(一)
  • Mac怎么远程控制Windows?
  • SpringBoot项目的5种搭建方式(以idea2017为例)
  • 敏感词 v0.24.0 新特性支持标签分类,内置实现多种策略
  • LabVIEW数字式气压计自动检定系统
  • 计算机视觉-人工智能(AI)入门教程一
  • PyCharm专项训练4 最小生成树算法
  • MySQL 数据”丢失”事件之 binlog 解析应用
  • 【Java 数据结构】移除链表元素
  • 某家政小程序系统 httpRequest 任意文件读取
  • 【ChatGPT】OpenAI 如何使用流模式进行回答
  • VSCode 插件开发实战(六):配置自定义状态栏
  • uniapp开发微信小程序笔记12-uniapp中使用Pinia
  • 【Python高级353】python实现多线程版本的TCP服务器
  • 16_HTML5 语义元素 --[HTML5 API 学习之旅]
  • Transformer 架构对比:Dense、MoE 与 Hybrid-MoE 的优劣分析
  • RAGFlow 基于深度文档理解构建的开源 RAG引擎 - 安装部署
  • Redisson 框架详解
  • FFTW基本概念与安装使用
  • Linux -- 同步与条件变量
  • 在线excel编辑(luckysheet)