码迷,mamicode.com
首页 > 数据库 > 详细

Oracle数据库驱动类DBHelper

时间:2016-01-07 18:30:58      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:oracle数据库驱动类dbhelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using Oracle.DataAccess.Client;

namespace DAL
{
    public class DBHelper
    {
        //public static string ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;//方式一,数据库链接串写在配置文件中
        public static string ConnectionString = "Data Source=ORCL;User ID=bonus;Password=bonus123";//方式二,直接写在cs文件中


        #region 对于所有表 ExecuteNonQuery() 直接对数据进行更新操作

        public static int ExecuteNonQuery(string sql)
        {
            return ExecuteNonQuery(sql, null, CommandType.Text);
        }

        public static int ExecuteNonQuery(string sql, OracleParameter[] para)
        {
            return ExecuteNonQuery(sql, para, CommandType.Text);
        }

        public static int ExecuteNonQuery(string sql, CommandType cmdType)
        {
            return ExecuteNonQuery(sql, null, cmdType);
        }
        public static int ExecuteNonQuery(string sql, OracleParameter[] para, CommandType cmdType)
        {
            int rows = -1;
            using (OracleConnection conn = new OracleConnection(ConnectionString))
            {
                OracleCommand cmd = new OracleCommand(sql, conn);
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                cmd.CommandType = cmdType;

                conn.Open();
                rows = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                conn.Close();
            }
            return rows;
        }

        #endregion

        #region 对于所有表 ExecuteReader 返回结果集

        public static OracleDataReader ExecuteReader(string sql)
        {
            return ExecuteReader(sql, null, CommandType.Text);
        }

        public static OracleDataReader ExecuteReader(string sql, OracleParameter[] para)
        {
            return ExecuteReader(sql, para, CommandType.Text);
        }
        public static OracleDataReader ExecuteReader(string sql, CommandType cmdType)
        {
            return ExecuteReader(sql, null, cmdType);
        }

        public static OracleDataReader ExecuteReader(string sql, OracleParameter[] para, CommandType cmdType)
        {
            OracleCommand cmd = null;
            OracleConnection conn = new OracleConnection(ConnectionString); ;
            cmd = new OracleCommand(sql, conn);
            if (para != null)
            {
                cmd.Parameters.AddRange(para);
            }
            cmd.CommandType = cmdType;
            conn.Open();
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        #endregion


        #region 对于所有表 ExecuteDataTable 返回数据表

        public static DataTable ExecuteDataTable(string sql)
        {
            return ExecuteDataTable(sql, null, CommandType.Text);
        }

        public static DataTable ExecuteDataTable(string sql, OracleParameter[] para)
        {
            return ExecuteDataTable(sql, para, CommandType.Text);
        }
        public static DataTable ExecuteDataTable(string sql, CommandType cmdType)
        {
            return ExecuteDataTable(sql, null, cmdType);
        }

        public static DataTable ExecuteDataTable(string sql, OracleParameter[] para, CommandType cmdType)
        {
            try
            {
                DataTable myTable = new DataTable();
                using (OracleConnection conn = new OracleConnection(ConnectionString))
                {
                    OracleCommand cmd = new OracleCommand(sql, conn);
                    if (para != null)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandType = cmdType;

                    OracleDataAdapter da = new OracleDataAdapter(cmd);
                    da.Fill(myTable);
                }
                return myTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region 对于所有表 ExecuteScalar 返回数据表
        public static object ExecuteScalar(string sql)
        {
            return ExecuteScalar(sql, null, CommandType.Text);
        }

        public static object ExecuteScalar(string sql, OracleParameter[] para)
        {
            return ExecuteScalar(sql, para, CommandType.Text);
        }

        public static object ExecuteScalar(string sql, CommandType cmdType)
        {
            return ExecuteScalar(sql, null, cmdType);
        }

        public static object ExecuteScalar(string sql, OracleParameter[] para, CommandType cmdType)
        {
            object res = null;
            using (OracleConnection conn = new OracleConnection(ConnectionString))
            {
                OracleCommand cmd = new OracleCommand(sql, conn);
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                cmd.CommandType = cmdType;
                conn.Open();
                res = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                conn.Close();
            }
            return res;
        }

        #endregion


        #region 存储过程操作
        /// <summary>  
        /// 执行存储过程  
        /// </summary>  
        /// <param name="storedProcName">存储过程名</param>  
        /// <param name="parameters">存储过程参数</param>  
        /// <returns>OracleDataReader返回数据集</returns>  
        public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            OracleConnection connection = new OracleConnection(ConnectionString);
            OracleDataReader returnReader;
            connection.Open();
            OracleCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.CommandType = CommandType.StoredProcedure;
            returnReader = command.ExecuteReader();
            return returnReader;
        }
        /// <summary>  
        /// 执行存储过程  
        /// </summary>  
        /// <param name="storedProcName">存储过程名</param>  
        /// <param name="parameters">存储过程参数</param>  
        /// <param name="tableName">DataSet结果中的表名</param>  
        /// <returns>DataSet</returns>  
        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
        {
            using (OracleConnection connection = new OracleConnection(ConnectionString))
            {
                DataSet dataSet = new DataSet();
                connection.Open();
                OracleDataAdapter sqlDA = new OracleDataAdapter();
                sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
                sqlDA.Fill(dataSet, tableName);
                connection.Close();
                return dataSet;
            }
        }
        /// <summary>  
        /// 构建 OracleCommand 对象(用来返回一个结果集,而不是一个整数值)  
        /// </summary>  
        /// <param name="connection">数据库连接</param>  
        /// <param name="storedProcName">存储过程名</param>  
        /// <param name="parameters">存储过程参数</param>  
        /// <returns>OracleCommand</returns>  
        private static OracleCommand BuildQueryCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            OracleCommand command = new OracleCommand(storedProcName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (OracleParameter parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }
            return command;
        }
 
     
        /// <summary>  
        /// 创建 OracleCommand 对象实例(存储过程返回输出参数)   
        /// </summary>  
        /// <param name="storedProcName">存储过程名</param>  
        /// <param name="parameters">存储过程参数</param>  
        /// <returns>OracleCommand 对象实例</returns>  
        /// 调用此方法的例子
        /* OracleParameter[] parameter = {  
              new OracleParameter("starttime",OracleDbType.Varchar2,8),  
                new OracleParameter("endtime",OracleDbType.Varchar2,8),
                new OracleParameter("days",OracleDbType.Decimal,3)
                };
                parameter[0].Direction = ParameterDirection.Input;
                parameter[1].Direction = ParameterDirection.Input;
                parameter[2].Direction = ParameterDirection.Output;
                //new OracleParameter("t_sql", OracleType.Cursor)};
                parameter[0].Value = starttime;
                parameter[1].Value = endtime;
       
                // parameter[2].Direction = System.Data.ParameterDirection.Output;
                 command = DBHelper.RunProcedureCommand("pro_comovertimedays", parameter);

                    //接收函数返回值
                  over_days = Convert.ToString(parameter[2].Value);//res
    */
        public static OracleCommand RunProcedureCommand(string storedProcName, IDataParameter[] parameters)
        {
            using (OracleConnection connection = new OracleConnection(ConnectionString))
            {
                
                OracleCommand command = new OracleCommand(storedProcName, connection);
                command.CommandType = CommandType.StoredProcedure;
                foreach (OracleParameter parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
                return command;               
                
            }
        }
        #endregion   
 
    }
}

本文出自 “11075795” 博客,谢绝转载!

Oracle数据库驱动类DBHelper

标签:oracle数据库驱动类dbhelper

原文地址:http://11085795.blog.51cto.com/11075795/1732556

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!