c#oracle连接

    /// <summary>
    /// EflowOracleAccess
    /// </summary>
    public class EflowOracleAccess
    {
        /// <summary>
        /// コネクション
        /// </summary>
        private OracleConnection _oraConn = null;

        /// <summary>
        /// トランザクション
        /// </summary>
        private OracleTransaction _oraTrans = null;

        /// <summary>
        /// コネクション
        /// </summary>
        public OracleConnection OraConnection
        {
            get { return _oraConn; }
            set { _oraConn = value; }
        }

        /// <summary>
        /// トランザクション
        /// </summary>
        public OracleTransaction OraTransaction
        {
            get { return _oraTrans; }
            set { _oraTrans = value; }
        }

        /// <summary>
        /// EflowOracleAccess
        /// </summary>
        public EflowOracleAccess()
        {
            this.OraConnection = new OracleConnection(Constant.DBConnection);
        }

        #region SELECT用のSQLメソッド

        /// <summary>
        /// SELECT用のSQLメソッド
        /// </summary>
        /// <param name="strSql">SELECT用のSQL</param>
        /// <returns>取得結果</returns>
        public DataTable ExecuteQuery(string strSql)
        {
            OracleDataReader Ora;
            try
            {
                this.OpenOraConnection();

                strSql = strSql.Replace(char.ConvertFromUtf32(2), "{");
                strSql = strSql.Replace(char.ConvertFromUtf32(3), "}");

                OracleCommand command = new OracleCommand(strSql, this.OraConnection);
                command.CommandType = System.Data.CommandType.Text;
                Ora = command.ExecuteReader();

                DataTable dtt = new DataTable();
                dtt.Load(Ora, LoadOption.OverwriteChanges);

                return dtt;
            }
            catch (OracleException oe)
            {
                throw oe;
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                this.OraConnection.Dispose();
                GC.Collect();
            }
        }

        /// <summary>
        /// SELECT用のSQLメソッド
        /// </summary>
        /// <param name="strSql">SELECT用のSQL</param>
        /// <returns>取得結果</returns>
        public object ExecuteScalar(string strSql)
        {
            try
            {
                this.OpenOraConnection();

                strSql = strSql.Replace(char.ConvertFromUtf32(2), "{");
                strSql = strSql.Replace(char.ConvertFromUtf32(3), "}");

                OracleCommand command = new OracleCommand(strSql, this.OraConnection);
                command.CommandType = System.Data.CommandType.Text;

                return command.ExecuteScalar();
            }
            catch (OracleException oe)
            {
                throw oe;
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                this.OraConnection.Dispose();
                GC.Collect();
            }
        }

        #endregion


        #region 更新系SQLメソッド
        /// <summary>
        /// 更新系(Update/Insert/Delete文)SQL発行用のSQL
        /// </summary>
        /// <param name="strSql">発行用のSQL</param>
        /// <returns>更新されたレコード数</returns>
        public int ExecuteNonQuery(string strSql)
        {
            try
            {
                this.OpenOraConnection();

                strSql = strSql.Replace(char.ConvertFromUtf32(2), "{");
                strSql = strSql.Replace(char.ConvertFromUtf32(3), "}");

                OracleCommand command = new OracleCommand(strSql, this.OraConnection);
                int intCount = command.ExecuteNonQuery();

                return intCount;
            }
            catch (OracleException oe)
            {
                throw oe;
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                this.OraConnection.Dispose();
                GC.Collect();
            }
        }

        /// <summary>
        /// 複数件のSQLを実行する
        /// </summary>
        /// <param name="strSql">発行用のSQL</param>
        /// <returns>更新されたレコード数</returns>
        public int ExecuteNonQuery(string[] strSql)
        {
            int intCount = 0;

            try
            {
                this.OpenOraConnection();

                for (int i = 0; i < strSql.Length; i++)
                {
                    string sql = strSql[i];

                    sql = sql.Replace(char.ConvertFromUtf32(2), "{");
                    sql = sql.Replace(char.ConvertFromUtf32(3), "}");

                    OracleCommand command = new OracleCommand(sql, this.OraConnection);
                    int j = command.ExecuteNonQuery();
                    //更新対象がなかった場合
                    if (j != 0)
                    {
                        intCount++;
                    }
                }

                return intCount;
            }
            catch (OracleException oe)
            {
                throw oe;
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                this.OraConnection.Dispose();
                GC.Collect();
            }
        }

        #endregion

        #region Transaction

        /// <summary>
        /// トランザクション開始
        /// </summary>
        public void BeginTrans()
        {
            try
            {
                //CheckConState
                this.OraTransaction = this.OraConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// OraConnection
        /// </summary>
        private void OpenOraConnection()
        {
            if (String.IsNullOrEmpty(this.OraConnection.ConnectionString))
            {
                this.OraConnection = new OracleConnection(Constant.DBConnection);
            }

            this.OraConnection.Open();
        }

        /// <summary>
        /// コミット
        /// </summary>
        public void Commit()
        {
            try
            {
                this.OraTransaction.Commit();
                this.OraTransaction = this.OraConnection.BeginTransaction();
            }
            catch (OracleException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// ロールバック
        /// </summary>
        public void RollBack()
        {
            try
            {
                if (this.OraTransaction != null && this.OraConnection != null)
                {
                    this.OraTransaction.Rollback();
                    this.OraTransaction = this.OraConnection.BeginTransaction();
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// コネクションの状態を確認する
        /// </summary>
        private void CheckConState()
        {
            try
            {
                if (this.OraConnection == null)
                {
                    this.OraConnection = new OracleConnection(Constant.DBConnection);
                }

                if (this.OraConnection.State == ConnectionState.Closed)
                {
                    this.OraConnection.Open();
                }
            }
            catch (OracleException oe)
            {
                throw oe;
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        #endregion
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值