c#oracle连接

    技术2022-05-11  11

        /// <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)