using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace YariSoft.DBUtil{
public class DBSchemaOperation
{
#region Protected variables
protected OleDbConnection connection = null;
protected string strSQL = "";
protected bool _startTransaction = true;
protected OleDbTransaction _transaction = null;
#endregion
#region Properties
public bool StartTransaction
{
set{ this._startTransaction = value; }
get{ return this._startTransaction; }
}
public OleDbTransaction Transaction {
set{ this._transaction = value; }
get{ return this._transaction; }
}
#endregion
#region Constructor/Destructor
public DBSchemaOperation()
{
}
#endregion
#region Public functions
public bool Exec()
{
bool status = false;
ConnectionState previousConnectionState = this.connection.State;
try{
if( previousConnectionState == ConnectionState.Closed ){
this.connection.Open();
}
if( this._startTransaction ){
this._transaction = this.connection.BeginTransaction(System.Data.IsolationLevel.Serializable);
}
status = this.ExecOperation();
}catch( Exception Exp ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
status = false;
}
if( this._startTransaction ){
if( this._transaction != null ){
if(status){
this._transaction.Commit();
}else{
this._transaction.Rollback();
}
}
}
if( previousConnectionState == ConnectionState.Closed ){
this.connection.Close();
}
return status;
}
public virtual bool PrepareSQL()
{
return true;
}
#endregion
#region Protected functions
protected virtual bool ExecOperation()
{
bool status = false;
OleDbCommand command = null;
try{
status = PrepareSQL();
if( ! status ){
return status;
}
if( this.strSQL.Trim() != "" ){
if( this._transaction != null ){
command = new OleDbCommand( this.strSQL, this.connection, this._transaction );
} else {
command = new OleDbCommand( this.strSQL, this.connection );
}
if( command != null ){
command.ExecuteNonQuery();
}
}
}catch( Exception Exp ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
status = false;
}
if( command != null ){
command.Dispose();
}
return status;
}
#endregion
}
}
|