⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datacopyoperation.cs

📁 一个通用的数据库访问层
💻 CS
字号:
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace YariSoft.DBUtil
{
	public class DataCopyOperation : DBBaseOperation
	{
		public delegate bool OnNeedInsertIdentityEventHandler( DataTable TableToUpdate, DataRow RowToUpdate, ref bool Updated );

		public event OnNeedInsertIdentityEventHandler
												OnNeedInsertIdentity = null;

		#region Local variables
		private RowOperation prevRowOperation = RowOperation.RO_None;
		#endregion

		#region Properties
		#endregion

		#region Constructor/Destructor
		public DataCopyOperation( OleDbConnection Connection, DataView Source, DataView Destination, System.Collections.ArrayList SelectedRows ):
			base ( Connection, Source, SelectedRows ) 
		{
			this.destination = Destination;
		}
		#endregion

		#region Protected functions
		protected override string GetProgressCaption()
		{
			return "Copy rows";
		}
		protected override string GetProgressMessage()
		{
			return "Copy rows to '" + this.destination.Table.TableName + "' table";
		}
		protected override bool ExecOperation( int Position )
		{
			bool status =  true;

			DataRow foundRow = null;
			int index = ( int )this.selectedRows[Position];

			RowOperation operation = this.CheckKeys( index, ref foundRow );
			if( this.prevRowOperation != RowOperation.RO_Overwrite_All && this.prevRowOperation != RowOperation.RO_Skip_All ){
				this.prevRowOperation = operation;
			}

			if( operation == RowOperation.RO_None ){
				return false;
			}

			bool rowUpdated = false;
			switch( operation ){
				case RowOperation.RO_Skip:
				case RowOperation.RO_Skip_All:	
					return true;
				case RowOperation.RO_Append:	
					foundRow = this.destination.Table.NewRow();
					for( int j = 0; j < this.destination.Table.Columns.Count; j++ ){
						if( !this.destination.Table.Columns[j].AutoIncrement && !this.destination.Table.Columns[j].ReadOnly ){
							foundRow[ j ] = this.source[index][j];
						}
					}
					this.destination.Table.Rows.Add( foundRow );
					if( this.OnNeedInsertIdentity != null ){
						if( ! this.OnNeedInsertIdentity ( this.destination.Table, foundRow, ref rowUpdated ) ){
							status = false;
						}
					}
					
					break;
				case RowOperation.RO_Overwrite:	
				case RowOperation.RO_Overwrite_All:	
					for( int j = 0; j < this.destination.Table.Columns.Count; j++ ){
						if(	   !this.destination.Table.Columns[j].AutoIncrement 
							&& !this.destination.Table.Columns[j].ReadOnly  ){

							foundRow[ j ] = this.source[index][j];
						}
					}
					break;
			}
			if( rowUpdated ){
				foundRow.AcceptChanges();
			}
			return status;
		}
		#endregion

		#region Private functions
		private RowOperation CheckKeys( int RowID, ref DataRow FoundRow )
		{
			RowOperation result = RowOperation.RO_Append;

			foreach( Constraint constr in this.destination.Table.Constraints ){
				if( constr is UniqueConstraint ){
					UniqueConstraint uConstr = ( UniqueConstraint )constr;
					object[]findTheseVals = new object[ uConstr.Columns.Length ];
					for( int i = 0; i < uConstr.Columns.Length; i++ ){
						findTheseVals[i] = this.source[RowID][uConstr.Columns[i].ColumnName];
					}

					FoundRow = this.destination.Table.Rows.Find( findTheseVals );
					if( FoundRow != null ){
						if( this.prevRowOperation != RowOperation.RO_Overwrite_All && this.prevRowOperation != RowOperation.RO_Skip_All ){
							OverwriteDataForm dialog = new OverwriteDataForm();
							result = dialog.ShowDialog( this.source[RowID].Row, FoundRow );
							dialog.Dispose();
						} else {
							result = this.prevRowOperation;
						}
					}
				}
			}
			return result;		
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -