OracleHelper--> Oracle数据库连接类及测试

请先下载对应的 odp.net 的dll, 放在 bin 目录下面。 下载地址:点击打开链接


1. OracleHelper.cs 

using System;
using System.Data;
using Oracle.DataAccess.Client;

namespace Utils
{
    /// 
    /// Desciption: Oracle数据库访问类 (注:针对 odp.net ). 
    /// Author    : yenange
    /// Date      : 2013-09-21
    /// 
    public static class OracleHelper
    {
        #region [ 连接对象 ]
        /// 
        /// 连接对象 字段
        /// 
        private static OracleConnection conn = null;
        /// 
        /// 连接串 字段
        /// 

        //private static string connstr = @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));User Id=leaf;Password=leaf;";
        private static string connstr = @"Data Source=localhost/ORCL;Persist Security Info=True;User ID=leaf;Password=leaf;";

        /// 
        /// 取得连接串
        /// 
        public static string GetConnectionString
        {
            get
            {
                return connstr;
            }
        }

        /// 
        /// 取得连接对象, 没有打开
        /// 
        public static OracleConnection GetOracleConnection
        {
            get
            {
                return new OracleConnection(GetConnectionString);
            }
        }

        /// 
        /// 取得连接对象, 并打开
        /// 
        public static OracleConnection GetOracleConnectionAndOpen
        {
            get
            {
                OracleConnection conn = GetOracleConnection;
                conn.Open();
                return conn;
            }
        }

        /// 
        /// 彻底关闭并释放 OracleConnection 对象,再置为null.
        /// 
        /// OracleConnection
        public static void CloseOracleConnection(OracleConnection conn)
        {
            if (conn == null)
                return;
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        #endregion

        #region [ ExecuteNonQuery ]
        /// 
        /// 普通SQL语句执行增删改
        /// 
        /// SQL语句
        /// 可变参数
        /// 受影响行数
        public static int ExecuteNonQuery(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteNonQuery(cmdText, CommandType.Text, commandParameters);
        }
        /// 
        /// 存储过程执行增删改
        /// 
        /// 存储过程
        /// 可变参数
        /// 受影响行数
        public static int ExecuteNonQueryByProc(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteNonQuery(cmdText, CommandType.StoredProcedure, commandParameters);
        }
        /// 
        /// 执行增删改
        /// 
        /// 命令字符串
        /// 命令类型
        /// 可变参数
        /// 受影响行数
        public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params OracleParameter[] commandParameters)
        {
            int result = 0;
            OracleConnection conn = null;

            try
            {
                conn = GetOracleConnectionAndOpen;
                OracleCommand command = new OracleCommand();
                PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                result = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                result = -1;
            }
            finally 
            {
                if (conn != null)
                    CloseOracleConnection(conn);
            }

            return result;
        }
        #endregion

        #region [ ExecuteReader ]
        /// 
        /// SQL语句得到 OracleDataReader 对象
        /// 
        /// 命令字符串
        /// 可变参数
        /// OracleDataReader 对象
        public static OracleDataReader ExecuteReader(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteReader(cmdText, CommandType.Text, commandParameters);
        }
        /// 
        /// 存储过程得到 OracleDataReader 对象
        /// 
        /// 命令字符串
        /// 可变参数
        /// OracleDataReader 对象
        public static OracleDataReader ExecuteReaderByProc(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteReader(cmdText, CommandType.StoredProcedure, commandParameters);
        }
        /// 
        /// 得到 OracleDataReader 对象
        /// 
        /// 命令字符串
        /// 命令类型
        /// 可变参数
        /// OracleDataReader 对象
        public static OracleDataReader ExecuteReader(string cmdText, CommandType cmdType, params OracleParameter[] commandParameters)
        {
            OracleDataReader result = null;
            OracleConnection conn = null;

            try
            {
                conn = GetOracleConnectionAndOpen;
                OracleCommand command = new OracleCommand();
                PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                result = command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                result = null;
            }
            finally
            {
                if (conn != null)
                    CloseOracleConnection(conn);
            }

            return result;
        }
        #endregion

        #region [ ExecuteScalar ]
        /// 
        /// 执行SQL语句, 返回Object
        /// 
        /// 命令字符串
        /// 可变参数
        ///  Object 
        public static Object ExecuteScalar(string cmdText, params OracleParameter[] commandParameters)
        { 
            return ExecuteScalar(cmdText, CommandType.Text, commandParameters);
        }

        /// 
        /// 执行存储过程, 返回Object
        /// 
        /// 命令字符串
        /// 可变参数
        ///  Object 
        public static Object ExecuteScalarByProc(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteScalar(cmdText, CommandType.StoredProcedure, commandParameters);
        }

        /// 
        /// 返回Object
        /// 
        /// 命令字符串
        /// 命令类型
        /// 可变参数
        ///  Object 
        public static Object ExecuteScalar(string cmdText, CommandType cmdType, params OracleParameter[] commandParameters)
        {
            Object result = null;
            OracleConnection conn = null;

            try
            {
                conn = GetOracleConnectionAndOpen;
                OracleCommand command = new OracleCommand();

                PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                result = command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                result = null;
            }
            finally
            {
                if (conn != null)
                    CloseOracleConnection(conn);
            }

            return result;
        } 
        #endregion

        #region [ ExecuteDataSet ]
        /// 
        /// 执行SQL语句, 返回DataSet
        /// 
        /// 命令字符串
        /// 可变参数
        ///  DataSet 
        public static DataSet ExecuteDataSet(string cmdText, params OracleParameter[] commandParameters)
        { 
            return ExecuteDataSet(cmdText, CommandType.Text, commandParameters);
        }

        /// 
        /// 执行存储过程, 返回DataSet
        /// 
        /// 命令字符串
        /// 可变参数
        ///  DataSet 
        public static DataSet ExecuteDataSetByProc(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteDataSet(cmdText, CommandType.StoredProcedure, commandParameters);
        }

        /// 
        /// 返回DataSet
        /// 
        /// 命令字符串
        /// 命令类型
        /// 可变参数
        ///  DataSet 
        public static DataSet ExecuteDataSet(string cmdText, CommandType cmdType, params OracleParameter[] commandParameters)
        {
            DataSet result = null;
            OracleConnection conn = null;

            try
            {
                conn = GetOracleConnectionAndOpen;
                OracleCommand command = new OracleCommand();

                PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                OracleDataAdapter adapter = new OracleDataAdapter();
                adapter.SelectCommand = command;
                result = new DataSet();
                adapter.Fill(result);
            }
            catch (Exception ex)
            {
                result = null;
            }
            finally
            {
                if (conn != null)
                    CloseOracleConnection(conn);
            }

            return result;
        }
        #endregion

        #region [ ExecuteDataTable ]
        /// 
        /// 执行SQL语句, 返回DataTable
        /// 
        /// 命令字符串
        /// 可变参数
        ///  DataTable 
        public static DataTable ExecuteDataTable(string cmdText, params OracleParameter[] commandParameters)
        { 
            return ExecuteDataTable(cmdText, CommandType.Text , commandParameters);
        }

        /// 
        /// 执行存储过程, 返回DataTable
        /// 
        /// 命令字符串
        /// 可变参数
        ///  DataTable 
        public static DataTable ExecuteDataTableByProc(string cmdText, params OracleParameter[] commandParameters)
        {
            return ExecuteDataTable(cmdText, CommandType.StoredProcedure, commandParameters);
        }

        /// 
        /// 返回DataTable
        /// 
        /// 命令字符串
        /// 命令类型
        /// 可变参数
        ///  DataTable 
        public static DataTable ExecuteDataTable(string cmdText, CommandType cmdType, params OracleParameter[] commandParameters)
        {
            DataTable dtResult = null;
            DataSet ds = ExecuteDataSet(cmdText, cmdType, commandParameters);

            if (ds != null && ds.Tables.Count > 0)
            {
                dtResult = ds.Tables[0];
            }
            return dtResult;
        }
        #endregion

        #region [ PrepareCommand ]
        /// 
        /// Command对象执行前预处理
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private static void PrepareCommand(OracleCommand command, OracleConnection connection, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
        {
            try
            {
                if (connection.State != ConnectionState.Open) connection.Open();

                command.Connection = connection;
                command.CommandText = cmdText;
                command.CommandType = cmdType;

                //if (trans != null) command.Transaction = trans;

                if (commandParameters != null)
                {
                    foreach (OracleParameter parm in commandParameters)
                        command.Parameters.Add(parm);
                }
            }
            catch
            {

            }
        }
        #endregion
    }//end of class
}//end of namespace


2. 测试类

using System;
using System.Collections.Generic;
using System.Text;
using Oracle.DataAccess.Client;
using System.Data;
using Utils;

namespace OracleForNet
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (OracleConnection conn = Utils.OracleHelper.GetOracleConnectionAndOpen)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        Console.WriteLine("打开连接成功!");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("出现异常, 异常信息: " + ex.Message);
            }
            Console.Read();
        }
    }//end of class
}//end of namespace

你可能感兴趣的:(DB,-,Oracle)