C#工具類OracleHelper,基於Oracle.ManagedDataAccess.Client封裝

基於Oracle.ManagedDataAccess.Client封裝的Oracle工具類OracleHelper,代碼以下:數據庫

using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.IO;
using Oracle.ManagedDataAccess.Client;
//using System.Data.OracleClient;    System.Data.OracleClient類已經不推薦使用

namespace Fly.Util.DataBase
{
    /// <summary>
    /// Oracle數據庫操做類
    /// </summary>
    public static class OracleHelper
    {
        /// <summary>  
        /// 執行數據庫非查詢操做,返回受影響的行數  
        /// </summary>  
        /// <param name="connectionString">數據庫鏈接字符串</param>
        /// <param name="cmdType">命令的類型</param>
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前操做影響的數據行數</returns>  
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            OracleCommand cmd = new OracleCommand();
            using (OracleConnection conn = new OracleConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
        }

        /// <summary>  
        /// 執行數據庫事務非查詢操做,返回受影響的行數  
        /// </summary>  
        /// <param name="transaction">數據庫事務對象</param>  
        /// <param name="cmdType">Command類型</param>  
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前事務操做影響的數據行數</returns>  
        public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            OracleCommand cmd = new OracleCommand();
            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }

        /// <summary>  
        /// 執行數據庫非查詢操做,返回受影響的行數  
        /// </summary>  
        /// <param name="connection">Oracle數據庫鏈接對象</param>  
        /// <param name="cmdType">Command類型</param>  
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前操做影響的數據行數</returns>  
        public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            if (connection == null)
                throw new ArgumentNullException("當前數據庫鏈接不存在");
            OracleCommand cmd = new OracleCommand();
            PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }

        /// <summary>  
        /// 執行數據庫查詢操做,返回OracleDataReader類型的內存結果集  
        /// </summary>  
        /// <param name="connectionString">數據庫鏈接字符串</param>
        /// <param name="cmdType">命令的類型</param>
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前查詢操做返回的OracleDataReader類型的內存結果集</returns>  
        public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            OracleCommand cmd = new OracleCommand();
            OracleConnection conn = new OracleConnection(connectionString);
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return reader;
            }
            catch
            {
                cmd.Dispose();
                conn.Close();
                throw;
            }
        }

        /// <summary>  
        /// 執行數據庫查詢操做,返回DataSet類型的結果集  
        /// </summary>  
        /// <param name="connectionString">數據庫鏈接字符串</param>
        /// <param name="cmdType">命令的類型</param>
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前查詢操做返回的DataSet類型的結果集</returns>  
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            OracleCommand cmd = new OracleCommand();
            OracleConnection conn = new OracleConnection(connectionString);
            DataSet ds = null;
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                OracleDataAdapter adapter = new OracleDataAdapter();
                adapter.SelectCommand = cmd;
                ds = new DataSet();
                adapter.Fill(ds);
                cmd.Parameters.Clear();
            }
            catch
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }

            return ds;
        }

        /// <summary>  
        /// 執行數據庫查詢操做,返回DataTable類型的結果集  
        /// </summary>  
        /// <param name="connectionString">數據庫鏈接字符串</param>
        /// <param name="cmdType">命令的類型</param>
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前查詢操做返回的DataTable類型的結果集</returns>  
        public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            OracleCommand cmd = new OracleCommand();
            OracleConnection conn = new OracleConnection(connectionString);
            DataTable dt = null;

            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                OracleDataAdapter adapter = new OracleDataAdapter();
                adapter.SelectCommand = cmd;
                dt = new DataTable();
                adapter.Fill(dt);
                cmd.Parameters.Clear();
            }
            catch
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }

            return dt;
        }

        /// <summary>  
        /// 執行數據庫查詢操做,返回結果集中位於第一行第一列的Object類型的值  
        /// </summary>  
        /// <param name="connectionString">數據庫鏈接字符串</param>
        /// <param name="cmdType">命令的類型</param>
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前查詢操做返回的結果集中位於第一行第一列的Object類型的值</returns>  
        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            OracleCommand cmd = new OracleCommand();
            OracleConnection conn = new OracleConnection(connectionString);
            object result = null;
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                result = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
            }
            catch
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }

            return result;
        }

        /// <summary>  
        /// 執行數據庫事務查詢操做,返回結果集中位於第一行第一列的Object類型的值  
        /// </summary>  
        /// <param name="trans">一個已存在的數據庫事務對象</param>  
        /// <param name="commandType">命令類型</param>  
        /// <param name="commandText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前事務查詢操做返回的結果集中位於第一行第一列的Object類型的值</returns>  
        public static object ExecuteScalar(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            if (trans == null)
                throw new ArgumentNullException("當前數據庫事務不存在");
            OracleConnection conn = trans.Connection;
            if (conn == null)
                throw new ArgumentException("當前事務所在的數據庫鏈接不存在");

            OracleCommand cmd = new OracleCommand();
            object result = null;

            try
            {
                PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);
                result = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
            }
            catch
            {
                throw;
            }
            finally
            {
                trans.Dispose();
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }

            return result;
        }

        /// <summary>  
        /// 執行數據庫查詢操做,返回結果集中位於第一行第一列的Object類型的值  
        /// </summary>  
        /// <param name="conn">數據庫鏈接對象</param>  
        /// <param name="cmdType">Command類型</param>  
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        /// <returns>當前查詢操做返回的結果集中位於第一行第一列的Object類型的值</returns>  
        public static object ExecuteScalar(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
        {
            if (conn == null) throw new ArgumentException("當前數據庫鏈接不存在");
            OracleCommand cmd = new OracleCommand();
            object result = null;

            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                result = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
            }
            catch
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }

            return result;
        }

        /// <summary>  
        /// 執行數據庫命令前的準備工做  
        /// </summary>  
        /// <param name="cmd">Command對象</param>  
        /// <param name="conn">數據庫鏈接對象</param>  
        /// <param name="trans">事務對象</param>  
        /// <param name="cmdType">Command類型</param>  
        /// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>  
        /// <param name="cmdParms">命令參數集合</param>  
        private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (OracleParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

        /// <summary>  
        /// 將.NET日期時間類型轉化爲Oracle兼容的日期時間格式字符串  
        /// </summary>  
        /// <param name="date">.NET日期時間類型對象</param>  
        /// <returns>Oracle兼容的日期時間格式字符串(如該字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>  
        public static string GetOracleDateFormat(DateTime date)
        {
            return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','YYYY-MM-DD')";
        }

        /// <summary>  
        /// 將.NET日期時間類型轉化爲Oracle兼容的日期格式字符串  
        /// </summary>  
        /// <param name="date">.NET日期時間類型對象</param>  
        /// <param name="format">Oracle日期時間類型格式化限定符</param>  
        /// <returns>Oracle兼容的日期時間格式字符串(如該字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>  
        public static string GetOracleDateFormat(DateTime date, string format)
        {
            if (format == null || format.Trim() == "") format = "YYYY-MM-DD";
            return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','" + format + "')";
        }

        /// <summary>  
        /// 將指定的關鍵字處理爲模糊查詢時的合法參數值  
        /// </summary>  
        /// <param name="source">待處理的查詢關鍵字</param>  
        /// <returns>過濾後的查詢關鍵字</returns>  
        public static string HandleLikeKey(string source)
        {
            if (source == null || source.Trim() == "") return null;

            source = source.Replace("[", "[]]");
            source = source.Replace("_", "[_]");
            source = source.Replace("%", "[%]");

            return ("%" + source + "%");
        }

        /// <summary>
        /// 執行存儲過程
        /// </summary>
        /// <param name="connection">SqlServer數據庫鏈接對象</param>  
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <returns>SqlDataReader對象</returns>
        public static OracleDataReader RunStoredProcedure(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            OracleDataReader returnReader = null;
            connection.Open();
            OracleCommand command = BuildSqlCommand(connection, storedProcName, parameters);
            returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
            return returnReader;
        }

        /// <summary>
        /// 構建SqlCommand對象
        /// </summary>
        /// <param name="connection">數據庫鏈接</param>
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <returns>SqlCommand</returns>
        private static OracleCommand BuildSqlCommand(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;
        }
    }
}
相關文章
相關標籤/搜索