OracleHelper类

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Oracle.DataAccess.Client;

namespace SZMobile.Portal.DAL
{

    /// <summary>
    /// Accesses oracle database helper class.
    /// </summary>
    public class OracleHelper
    {

        /// <summary>
        /// Executes oracle command.
        /// </summary>
        /// <param name="oracle">oracle command.</param>
        /// <returns>The row count which has been affected.</returns>
        public static int ExecuteNonQuery(string commandText)
        {
            if (string.IsNullOrEmpty(commandText))
            {
                return -1;
            }
            return ExecuteNonQuery(commandText, null);
        }

        /// <summary>
        /// Executes oracle command.
        /// </summary>
        /// <param name="oracle">oracle command.</param>
        /// <param name="parameters">The OracleParameter object related to oracleCommand object.</param>
        /// <returns>The row count which has been affected.</returns>
        public static int ExecuteNonQuery(string commandText, OracleParameter[] parameters)
        {
            CommandType commandType = CommandType.Text;
            return ExecuteNonQuery(commandType, commandText, parameters);
        }

        /// <summary>
        /// Execute a database query which does not include a select
        /// </summary>       
        /// <param name="commandType">Command type either stored procedure or oracle</param>
        /// <param name="commandText">Acutall oracle Command</param>
        /// <param name="commandParameters">Parameters to bind to the command</param>
        /// <returns>The row count which has been affected</returns>
        public static int ExecuteNonQuery(CommandType commandType,string commandText, OracleParameter[] commandParameters)
        {
            int i = -3;
            if (string.IsNullOrEmpty(commandText) && (commandParameters == null))
            {
                return i;
            }
            try
            {  
                //Create a connection
                using (OracleConnection connection = ConnectionManager.OpenConnection())
                {
                    using (OracleCommand command = new OracleCommand())
                    {
                        //Prepare the command
                        PrepareCommand(command, connection,commandType, commandText, commandParameters);

                        //Execute the command
                        i = command.ExecuteNonQuery();
                        command.Parameters.Clear();
                        return i;
                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionManager.CloseConnection();           
            }
        }

        /// <summary>
        /// Execute a select query that will return a result set
        /// </summary>       
        /// <param name="commandType">the CommandType (stored procedure, text)</param>
        /// <param name="commandText">the stored procedure name or PL/oracle command</param>
        /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
        /// <returns>retrurn a oracleDataReader if successful </returns>
        public static OracleDataReader ExecuteReader(CommandType commandType, string commandText, OracleParameter[] commandParameters)
        {
            try
            {
                //Create a connection
                using (OracleConnection connection = ConnectionManager.OpenConnection())
                {
                    using (OracleCommand command = new OracleCommand())
                    {
                        //Prepare the command
                        PrepareCommand(command, connection, commandType, commandText, commandParameters);

                        //Execute the reader
                        using (OracleDataReader oracleDataReader = command.ExecuteReader())
                        {
                            command.Parameters.Clear();
                            return oracleDataReader;
                        }
                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionManager.CloseConnection();
            }
        }

        /// <summary>
        /// Executes oracle command and returns DataSet object.
        /// </summary>
        /// <param name="oracle">oracle command.</param>
        /// <returns>The result stored in DataSet object.</returns>
        public static DataSet ExecuteDataSet(string commandText)
        {
            if (string.IsNullOrEmpty(commandText))
            {
                return null;
            }
            return ExecuteDataSet(commandText, null);
        }

        /// <summary>
        /// Executes oracle command and returns dataset object.
        /// </summary>
        /// <param name="oracle">oracle command.</param>
        /// <param name="parameters">oracleParameters related to oraclecommand object.</param>
        /// <returns>The result stored in DataSet object.</returns>
        public static DataSet ExecuteDataSet(string commandText, params OracleParameter[] parameters)
        {
            if (string.IsNullOrEmpty(commandText) && (parameters == null))
            {
                return null;
            }
            return ExecuteDataSet(CommandType.Text, commandText, parameters);
        }

        /// <summary>
        /// Executes oracle command and return DataSet object.
        /// </summary>
        /// <param name="commandText">oracleCommand text.</param>
        /// <param name="commandType">oracleCommand type.</param>
        /// <param name="parameters">oracleParameters related to oraclecommand object.</param>
        /// <returns>DataSet object.</returns>
        public static DataSet ExecuteDataSet(CommandType commandType, string commandText, params OracleParameter[] parameters)
        {
            try
            {
                using (OracleConnection connection = ConnectionManager.OpenConnection())
                {
                    using (OracleCommand command = new OracleCommand())
                    {

                        DataSet dataSet = new DataSet();
                        PrepareCommand(command, connection, commandType, commandText, parameters);
                        using (OracleDataAdapter adapter = new OracleDataAdapter(command))
                        {
                            adapter.Fill(dataSet);
                            command.Parameters.Clear();
                            return dataSet;
                        }
                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionManager.CloseConnection();
            }
        }

        /// <summary>
        /// Prepare Input type parameter for oracle command.
        /// </summary>
        /// <param name="paraName">The parameter's name.</param>
        /// <param name="dbType">The parameter's data type.</param>
        /// <param name="size">The parameter's size.</param>
        /// <param name="value">The parameter's default value.</param>
        /// <returns>Returns the Input type parameter.</returns>
        public static OracleParameter MakeParameter(string paraName, OracleDbType dbType, int size, object value)
        {
            OracleParameter parameter;
            if (size > 0)
            {
                parameter = new OracleParameter(paraName, dbType, size);
            }
            else
            {
                parameter = new OracleParameter(paraName, dbType);
            }

            if (null != value)
            {
                parameter.Value = value;
            }
            return parameter;
        }

        /// <summary>
        /// Prepares works before execution the command.
        /// </summary>
        /// <param name="command">The oracleCommand object.</param>
        /// <param name="connection">The oracleConnection object.</param>
        /// <param name="transaction">The oracleTransaction </param>
        /// <param name="commandType">The CommandType object.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="commandParameters">The command parameters related to the command.</param>
        public static void PrepareCommand(OracleCommand command, OracleConnection connection,CommandType commandType, string commandText, OracleParameter[] commandParameters)
        {
            if (command == null)
            {
                throw new ArgumentNullException();
            }

            if ((commandText == null) || (commandText.Length == 0))
            {
                throw new ArgumentNullException();
            }

            if (null != connection)
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            else
            {
                throw new ArgumentNullException();
            }
            command.Connection = connection;
            command.CommandText = commandText;           
            command.CommandType = commandType;
            if (commandParameters != null)
            {
                AttachParameters(command, commandParameters);
            }
        }

        /// <summary>
        /// Attaches oracle parameters object to oraclecommand object.
        /// </summary>
        /// <param name="command">oracle Command Object.</param>
        /// <param name="commandParameters">oracle parameters relates to the oracleCommand object.</param>
        public static void AttachParameters(OracleCommand command, OracleParameter[] commandParameters)
        {
            if (null == command)
            {
                throw new ArgumentNullException();
            }
            if (null != commandParameters)
            {
                foreach (OracleParameter parameter in commandParameters)
                {
                    if (null != parameter)
                    {
                        if (parameter.Value == null)
                        {
                            parameter.Value = DBNull.Value;
                        }
                        command.Parameters.Add(parameter);
                    }
                }
            }
        }

        /// <summary>
        /// Executes the procedure.
        /// </summary>
        /// <param name="name">The procedure name.</param>
        /// <param name="parameters">The parameters needs in the procedure.</param>
        /// <returns>Returns the lines affected.</returns>
        public static int RunProcedure(string name, OracleParameter[] parameters)
        {
            return ExecuteNonQuery(CommandType.StoredProcedure, name, parameters);
        }                   
       
       

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值