CSharp: Oracle Stored Procedure query table

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

oracle sql script:

sql 复制代码
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;
/
cs 复制代码
        /// <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:

cs 复制代码
       ///<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;
	}

调用:

cs 复制代码
    /// <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();
        }
    }

输出:

相关推荐
余~~1853816280012 分钟前
矩阵碰一碰发视频源码技术解析,支持OEM
数据库·microsoft
张声录143 分钟前
【ETCD】【实操篇(十五)】etcd集群成员管理:如何高效地添加、删除与更新节点
数据库·etcd
天乐敲代码43 分钟前
Etcd静态分布式集群搭建
数据库·分布式·etcd
chengma_09090944 分钟前
MySQL 数据库连接数查询、配置
数据库·mysql
TDengine (老段)1 小时前
两分钟掌握 TDengine 全部写入方式
大数据·数据库·时序数据库·tdengine·涛思数据
码农君莫笑1 小时前
《信管通低代码信息管理系统开发平台》Windows环境安装说明
服务器·数据库·windows·低代码·c#·bootstrap·.netcore
计算机学长felix2 小时前
基于SpringBoot的“大学生社团活动平台”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·后端
木与子不厌2 小时前
微服务自定义过滤器
运维·数据库·微服务