using System;
using System.Data;
using System.Data.OleDb;
using YariSoft.Utils;
using System.Windows.Forms;
using System.Collections;
namespace YariSoft.DBUtil{
public enum RowOperation {
RO_None,
RO_Append,
RO_Overwrite,
RO_Overwrite_All,
RO_Skip,
RO_Skip_All
}
public class DBBaseOperation
{
public delegate bool OnDataChangedEventHandler( object Sender );
public event OnDataChangedEventHandler OnSourceDataChanged = null;
public event OnDataChangedEventHandler OnDestinationDataChanged = null;
#region Protected variables
protected DataView source = null;
protected DataView destination = null;
protected OleDbConnection connection= null;
protected ArrayList selectedRows = null;
protected YProgress progress = null;
#endregion
#region Local variables
private bool _showProgress = true;
protected bool increaseProgress = true;
#endregion
#region Properties
public bool ShowProgress
{
set{ this._showProgress = value; }
get{ return this._showProgress; }
}
#endregion
#region Constructor/Destructor
public DBBaseOperation()
{
}
public DBBaseOperation( OleDbConnection Connection, ArrayList SelectedRows )
:this( Connection, null, SelectedRows )
{
}
public DBBaseOperation( OleDbConnection Connection, DataView Source, ArrayList SelectedRows )
{
this.source = Source;
this.selectedRows = SelectedRows;
this.connection = Connection;
}
~DBBaseOperation(){
this.Dispose();
}
public virtual void Dispose()
{
if( this.progress != null ){
this.progress.Dispose();
}
}
#endregion
#region Public functions
public virtual bool Exec()
{
bool status = true;
ConnectionState previousConnectionState = this.connection.State;
try{
if( previousConnectionState == ConnectionState.Closed ){
this.connection.Open();
}
this.PrepareProgress( this.selectedRows.Count, this.GetProgressCaption(), this.GetProgressMessage());
status = this.BeforeProcess();
if( this.increaseProgress ){
for( int i = 0; !this.progress.Cancel && status && i < this.selectedRows.Count; i++ ){
this.Process( i, ref status );
}
} else {
for( int i = this.selectedRows.Count - 1; !this.progress.Cancel && status && i >= 0; i-- ){
this.Process( i, ref status );
}
}
if( status ){
status = this.AfterProcess();
}
} catch ( Exception Exp ) {
string message = Exp.Message;
message += "\r\nTrace:\r\n";
message += Exp.StackTrace;
YariSoft.Utils.YMessages.Show( message, "Error ( IOperation.Exec )", MessageBoxButtons.OK, MessageBoxIcon.Error );
status = false;
}
if( previousConnectionState == ConnectionState.Closed ){
this.connection.Close();
}
return status;
}
#endregion
#region Protected functions
protected virtual string GetProgressCaption()
{
return "";
}
protected virtual string GetProgressMessage()
{
return "";
}
protected virtual bool ExecOperation( int Position )
{
return true;
}
protected virtual bool BeforeProcess()
{
return true;
}
protected virtual bool AfterProcess()
{
return true;
}
#endregion
#region Private functions
private void Process( int Position, ref bool Status )
{
Status = this.ExecOperation( Position );
if( ! Status ){
return;
}
this.SetProgressValue( Position );
if( this.OnSourceDataChanged != null ){
Status = this.OnSourceDataChanged ( this.source.Table );
}
if( this.OnDestinationDataChanged != null ){
Status = this.OnDestinationDataChanged ( this.destination.Table );
}
}
private void PrepareProgress( int Maximum, string ProgressFormText, string OperationText )
{
if( this.ShowProgress ){
this.progress = new YariSoft.Utils.YProgress ();
this.progress.Maximum = Maximum;
this.progress.Text = ProgressFormText;
this.progress.OperationCaption = OperationText;
}
}
private void SetProgressValue( int Value )
{
if( this.ShowProgress ){
this.progress.Value = Value;
}
}
#endregion
}
}
|