首页 > 数据库 >CSharp: Oracle Stored Procedure query table

CSharp: Oracle Stored Procedure query table

时间:2024-12-25 20:41:29浏览次数:7  
标签:school CSharp cmd new cursor Stored reader table null

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;
        }

  

       ///<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();
        }
    }

  

 

标签:school,CSharp,cmd,new,cursor,Stored,reader,table,null
From: https://www.cnblogs.com/geovindu/p/18631375

相关文章

  • ThreadLocal与InheritableThreadLocal
    ThreadLocal底层是个map每次set值的时候把当前线程与值放到里面ThreadLocal.ThreadLocalMapthreadLocals=null;   这种结构在大数据量并发请求时会,会产生内存泄漏。请求时set进去,正常退出move掉,来不及remove的数据会停留在内存中,外界还有引用,gc不会收......
  • 全网最全,保姆级Stable Diffusion系列入门使用教程下篇(图生图、LoRA、提示词权重)
    前言:在上一篇文章中,我们带领大家了解了StableDiffusion的基础操作。接下来,下篇教程将深入探讨图生图、LoRA、提示词权重等高级功能,为大家提供全网最全、保姆级的StableDiffusion使用指南,助你轻松驾驭这款强大的图像生成工具。让我们一起开启创作之旅!一、LoRA1、什么是L......
  • Linux防火墙(iptables)设置_prot opt source
    直接改iptables配置就可以了:vim/etc/sysconfig/iptables1、关闭所有的INPUTFORWARDOUTPUT只对某些端口开放。下面是命令实现:iptables-PINPUTDROPiptables-PFORWARDDROPiptables-POUTPUTDROP再用命令iptables-L-n查看是否设置好,好看到全部DROP了......
  • table的单元格如何保持宽度不变?
    在前端开发中,如果你想让的单元格(<td>)保持特定的宽度,你可以通过CSS来实现。以下是一些方法:使用内联样式或外部样式表设置固定宽度:你可以直接在<td>标签上使用style属性来设置宽度,或者在外部或内部样式表中为<td>定义一个类,并设置宽度。<!--内联样式示例--><tdstyl......
  • 解决element ui中table列合并
    1.创建一个空的数组存储处理的数据spanArr:[],//合并单元格2.创建处理方法//合并单元格getSpanArr(data){letpos=0;for(leti=0;i<data.length;i++){if(i===0){this.spanArr.push(1);pos=0;}......
  • 最新Stable Diffusion整合包安装,全新加速、解压即用、防爆显存三分钟入门
    StableDiffusion出来已经很长一段时间了,网上其实已经有很多很多教程教如何安装了,但是由于网上太多的教程,也不知道哪个更好,且有的伙伴可能也是没找到合适的,也有的伙伴暂时还没找到。在StableDiffusion刚出来不久,我也尝试了多种安装方式,也有试过google、腾讯云一键安装脚本......
  • 【stable diffusion教程】有没有人能详细介绍一下Stable Diffusion AI绘画?
    前言简单来说,StableDiffusion(简称SD)就是一个AI自动生成图片的软件,通过我们输入文字,SD就能生成对应的一张图片。很多人也想尝试,但被复杂的操作步骤劝退,今天我简单介绍一下关于SD的安装和使用教程。AI图像生成技术对硬件要求较高,特别是处理器显卡,电脑整体性能水平越高,......
  • 你有使用过ContentEditable属性吗?说说你对它的理解
    ContentEditable属性在前端开发中的理解与应用ContentEditable是HTML中的一个属性,用于指定元素内容是否可编辑。当该属性设置为"true"时,元素的内容变得可以编辑,用户可以直接在浏览器页面上修改内容。这种特性在前端开发中非常有用,尤其是在需要实现富文本编辑器或允许用户自定义页......
  • [CSS] Create a mobile friendly table with the before pseudo-element
    MakeanHTMLtablemorereadableonmobiledevicesbyusingthe::beforepseudo-element.Weaddlabelstoeachcell,sotheyappearlikecardswhenthetableisviewedonsmallerscreens.The::beforecontentissetusingadata-labelattributeforeachc......
  • 11. 说说Hashtable 与 HashMap 的区别
    出生的版本不一样,Hashtable出生于Java发布的第一版本JDK1.0,HashMap出生于JDK1.2。都实现了Map、Cloneable、Serializable(当前JDK版本1.8)。HashMap继承的是AbstractMap,并且AbstractMap也实现了Map接口。Hashtable继承Dictionary。Hashtable中大部分public......