using System; using System.Data; using System.Data.OracleClient; namespace Payroll.BO { /// /// Summary description for ORACLEHelper. /// public class ORACLEHelper:DABase { public ORACLEHelper() { } private static bool IsTransaction=false; private static void PrepareOracleConnectiion() { if(OracleConnection==null) { if(DABase.OracleStaticConnectionString !="") { OracleConnection=new System.Data.OracleClient.OracleConnection(DABase.OracleStaticConnectionString); if(OracleConnection ==null) throw new Exception("Cannot connect to ORACLE server"); } else throw new Exception("Cannot connect to ORACLE server"); } // If the provided connection is not open, we will open it if (OracleConnection.State!=ConnectionState.Open) { OracleConnection.Open(); } } public static void BeginOracleTran() { PrepareOracleConnectiion(); OracleTransaction=OracleConnection.BeginTransaction(); IsTransaction=true; } public static void CommitOracleTran() { if(OracleTransaction!=null) { OracleTransaction.Commit(); OracleTransaction=null; IsTransaction=false; } if(OracleConnection!=null) { if (OracleConnection.State==ConnectionState.Open) { OracleConnection.Close(); } } } public static void RollbackOracleTran() { if(OracleTransaction!=null) { OracleTransaction.Rollback(); OracleTransaction=null; IsTransaction=false; } if(OracleConnection!=null) { if (OracleConnection.State==ConnectionState.Open) { OracleConnection.Close(); } } } public static IDataReader ExecuteReader(string CommandText, params object[] parameterValues) { try { PrepareOracleConnectiion(); string sSQL=SQL.MakeSQL(CommandText,parameterValues); OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; return (IDataReader)oCMD.ExecuteReader(); } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } // finally // { // if(!IsTransaction && OracleConnection !=null) // OracleConnection.Close(); // // } } public static void CloseConnection() { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } public static int ExecuteNonQuery(string CommandText, params object[] parameterValues) { try { PrepareOracleConnectiion(); string sSQL=SQL.MakeSQL(CommandText,parameterValues); OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; return oCMD.ExecuteNonQuery(); } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } finally { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } } public static int ExecuteNonQuery(string spname, OracleParameter[] OracleParameters) { try { PrepareOracleConnectiion(); string sSQL=SQL.MakeSQL(DABase.DBUser + "." + spname);//GPSTSUSER OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; oCMD.CommandType=CommandType.StoredProcedure; AttachParameters(oCMD,OracleParameters); return oCMD.ExecuteNonQuery(); } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } finally { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } } public static OracleParameter GetParameter(ParameterDirection pdirection,OracleType datatype,object pValue) { OracleParameter parameter=new OracleParameter(); parameter.Direction=pdirection ; parameter.OracleType =datatype ; // if(datatype==OracleType.AnsiString) // {parameter.Size=20; } parameter.Value=pValue ; return parameter; } public static OracleParameter GetParameter(string parametername,ParameterDirection pdirection,OracleType datatype,object pValue) { OracleParameter parameter=new OracleParameter(); parameter.Direction=pdirection ; parameter.OracleType =datatype ; parameter.ParameterName=parametername; parameter.Value=pValue ; return parameter; } public static OracleParameter GetParameter(string parametername,ParameterDirection pdirection,OracleType datatype,int size,object pValue) { OracleParameter parameter=new OracleParameter(); parameter.ParameterName=parametername; parameter.Direction=pdirection ; parameter.OracleType =datatype ; parameter.Size=size; parameter.Value=pValue ; return parameter; } public static OracleParameter GetParameter(ParameterDirection pdirection,OracleType datatype,int size,object pValue) { OracleParameter parameter=new OracleParameter(); parameter.Direction=pdirection ; parameter.OracleType =datatype ; parameter.Size=size; parameter.Value=pValue ; return parameter; } private static void AttachParameters(OracleCommand command, OracleParameter[] commandParameters) { if( command == null ) throw new ArgumentNullException( "command" ); if( commandParameters != null ) { foreach (OracleParameter p in commandParameters) { if( p != null ) { // Check for derived output value with no value assigned if ( ( p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input ) && (p.Value == null || p.Value.ToString()=="")) { p.Value = DBNull.Value; } command.Parameters.Add(p); } } } } public static int ExecuteNonQuery(string CommandText) { try { PrepareOracleConnectiion(); string sSQL=CommandText; OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; return oCMD.ExecuteNonQuery(); } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } finally { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } } public static object ExecuteScalar(string CommandText, params object[] parameterValues) { try { PrepareOracleConnectiion(); string sSQL=SQL.MakeSQL(CommandText,parameterValues); OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; return oCMD.ExecuteScalar(); } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } finally { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } } public static object ExecuteScalar(string CommandText) { try { PrepareOracleConnectiion(); string sSQL=CommandText; OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; return oCMD.ExecuteScalar(); } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } finally { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } } public static int GenerateNewID(string TableName, string FieldName) { try { PrepareOracleConnectiion(); string sSQL=SQL.MakeSQL("SELECT MAX(%q) FROM %q",FieldName,TableName); OracleCommand oCMD=new OracleCommand(); if(IsTransaction) { oCMD.Connection=OracleConnection; oCMD.Transaction=OracleTransaction; } else { oCMD.Connection=OracleConnection; } oCMD.CommandText=sSQL; object obj= oCMD.ExecuteScalar(); if(obj == DBNull.Value)return 1; else if(obj==null) return 1; else return Convert.ToInt32(obj) + 1; } catch(Exception ex) { throw new Exception("Framework Error : " + ex.Message); } finally { if(!IsTransaction && OracleConnection !=null) OracleConnection.Close(); } } } }