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

📄 dbschemaoperation.cs

📁 一个通用的数据库访问层
💻 CS
字号:
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
	}
}

⌨️ 快捷键说明

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