TransactionHelper
public class TransactionHelper
{
public static OracleTransaction ora_Transaction = null;
public static OracleCommand ora_Command { get; set; }
public static OracleConnection ora_Conn = null;
private static string ls_XMLFile = “SysInfo.xml”;//配置文檔
private static string ls_SectionName = “”;
private static Action initAction = () =>
{
#region 初始化Transaction
if (string.IsNullOrEmpty(ls_SectionName)) ls_SectionName = “DBConn_EMES”;
ora_Command = new OracleCommand();
ora_Conn = OracleHelper.DBOpen_Ora(ls_SectionName);
ora_Conn.Open();
ora_Command.Connection = ora_Conn;
ora_Transaction = ora_Conn.BeginTransaction();
ora_Command.Transaction = ora_Transaction;
#endregion
};
private static Action disAction = () =>
{
#region 銷毀 Transaction
if (ora_Command != null) ora_Command.Dispose();
ora_Command = null; ora_Transaction = null;
if (ora_Conn != null)
{
if (ora_Conn.State == ConnectionState.Open) ora_Conn.Close();
ora_Conn.Dispose();
}
ora_Conn = null;
#endregion
};
public TransactionHelper()
{
try
{
if (ora_Conn == null)
{
initAction.Invoke();
}
}
catch (Exception ex)
{
StringBuilder lo_StringBuilder = new StringBuilder(ex.Message.ToString());
ECCHelper.ClassHelper.WriteLog(lo_StringBuilder);
disAction.Invoke();
}
//if (ora_Conn.State == ConnectionState.Open) ora_Conn.Close();
//ora_Conn.Dispose();
}
public void executeNoneQueryCommand(string sqlCommand)
{
try
{
ora_Command.CommandText = sqlCommand;
ora_Command.ExecuteNonQuery();
}
catch (Exception ex)
{
StringBuilder lo_StringBuilder = new StringBuilder(ex.Message.ToString());
ECCHelper.ClassHelper.WriteLog(lo_StringBuilder);
disAction.Invoke();
throw ex;
}
}
/// <summary>
/// 初始調用 OracleCommnad
/// </summary>
public OracleCommand InitCommand()
{
return ora_Command;
}
/// <summary>
/// 銷毀
/// </summary>
/// <returns></returns>
public bool CommitTransaction()
{
try
{
if (ora_Transaction != null)
{
ora_Transaction.Commit(); return true;
}
else { ora_Transaction.Rollback(); return false; }
}
catch (Exception ex)
{
if (ora_Transaction != null) ora_Transaction.Rollback();
StringBuilder lo_StringBuilder = new StringBuilder(ex.Message.ToString());
ECCHelper.ClassHelper.WriteLog(lo_StringBuilder);
return false;
}
finally
{
disAction.Invoke();
}
}
}