示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="commandBehavior"></param>
        /// <returns></returns>
        public DbDataReader ExecuteReader(DbCommand command, CommandBehavior commandBehavior)
        {
            DbDataReader   dr        = null;
            DbCommand      cm        = command;
            DateTimeOffset?startTime = null;
            DateTimeOffset?endTime   = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteReader, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(null);
                }

                this.Open();
                cm.Connection  = this.Connection;
                cm.Transaction = this.Transaction;
                startTime      = DateTimeOffset.Now;
                dr             = cm.ExecuteReader(commandBehavior);
                endTime        = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.Close();
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteReader, this.ConnectionString, ex, cm));
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteReader, this.ConnectionString, startTime.Value, endTime.Value, cm));
            }
            return(dr);
        }
示例#2
0
        public Int32 Save(DbDataAdapter dataAdapter, DataTable dataTable)
        {
            var            previousState       = ConnectionState;
            int            affectedRecordCount = -1;
            DateTimeOffset?startTime           = null;
            DateTimeOffset?endTime             = null;
            Object         ec = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.Save, ConnectionString, dataAdapter));
                if (e != null && e.Cancel == true)
                {
                    return(-1);
                }
                ec = e.ExecutionContext;

                Open();
                if (dataAdapter.InsertCommand != null)
                {
                    dataAdapter.InsertCommand.Connection  = this.Connection;
                    dataAdapter.InsertCommand.Transaction = this.Transaction;
                }
                if (dataAdapter.UpdateCommand != null)
                {
                    dataAdapter.UpdateCommand.Connection  = this.Connection;
                    dataAdapter.UpdateCommand.Transaction = this.Transaction;
                }
                if (dataAdapter.DeleteCommand != null)
                {
                    dataAdapter.DeleteCommand.Connection  = this.Connection;
                    dataAdapter.DeleteCommand.Transaction = this.Transaction;
                }
                startTime           = DateTimeOffset.Now;
                affectedRecordCount = dataAdapter.Update(dataTable);
                endTime             = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new CommandErrorEventArgs(MethodName.Save, ConnectionString, ex, ec, dataAdapter));
            }
            finally
            {
                if (previousState == ConnectionState.Closed &&
                    ConnectionState == ConnectionState.Open)
                {
                    Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.Save, ConnectionString
                                                                        , startTime.Value, endTime.Value, ec, dataAdapter));
            }
            return(affectedRecordCount);
        }
示例#3
0
        public Int32[] ExecuteCommand(IsolationLevel isolationLevel, params DbCommand[] commands)
        {
            var                       affectRecordNumber = new Int32[commands.Length];
            ConnectionState           state = this.ConnectionState;
            DbCommand                 cm    = null;
            CommandExecutingEventArgs e;
            Object                    ec = null;

            try
            {
                this.Open();
                this.BeginTransaction(isolationLevel);
                for (Int32 i = 0; i < commands.Length; i++)
                {
                    cm = commands[i];
                    e  = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteCommandList, this.ConnectionString, cm));
                    if (e != null && e.Cancel == true)
                    {
                        continue;
                    }
                    ec = e.ExecutionContext;

                    cm.Connection  = this.Connection;
                    cm.Transaction = this.Transaction;
                    var startTime = DateTimeOffset.Now;
                    affectRecordNumber[i] = cm.ExecuteNonQuery();
                    var endTime = DateTimeOffset.Now;
                    Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteCommandList, this.ConnectionString
                                                                            , startTime, endTime, ec, cm));
                }
                this.Transaction.Commit();
            }
            catch (Exception ex)
            {
                if (this.Connection.State == ConnectionState.Open)
                {
                    this.Transaction.Rollback();
                }
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteCommandList, this.ConnectionString, ex, ec, cm));
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            return(affectRecordNumber);
        }
示例#4
0
        public async Task <Int32> ExecuteCommandAsync(DbCommand command, CancellationToken cancellationToken)
        {
            var             affectRecordNumber = Int32.MinValue;
            ConnectionState state     = this.ConnectionState;
            DbCommand       cm        = command;
            DateTimeOffset? startTime = null;
            DateTimeOffset? endTime   = null;
            Object          ec        = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteCommand, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(-1);
                }
                ec = e.ExecutionContext;

                await this.OpenAsync();

                cm.Connection      = this.Connection;
                cm.Transaction     = this.Transaction;
                startTime          = DateTimeOffset.Now;
                affectRecordNumber = await cm.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);

                endTime = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteCommand, this.ConnectionString, ex, ec, cm));
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteCommand, this.ConnectionString
                                                                        , startTime.Value, endTime.Value, ec, cm));
            }
            return(affectRecordNumber);
        }
示例#5
0
        public DataTable GetDataTable(DbCommand command)
        {
            var            dt        = new DataTable();
            DbDataAdapter  da        = null;
            DbCommand      cm        = command;
            DateTimeOffset?startTime = null;
            DateTimeOffset?endTime   = null;
            Object         ec        = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.GetDataTable, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(null);
                }
                ec = e.ExecutionContext;

                this.Open();
                da               = this.CreateDataAdapter();
                cm.Connection    = this.Connection;
                cm.Transaction   = this.Transaction;
                da.SelectCommand = cm;
                startTime        = DateTimeOffset.Now;
                da.Fill(dt);
                endTime = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new CommandErrorEventArgs(MethodName.GetDataTable, this.ConnectionString, ex, ec, cm));
            }
            finally
            {
                if (cm.Transaction == null && this.Connection.State == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.GetDataTable, this.ConnectionString
                                                                        , startTime.Value, endTime.Value, ec, cm));
            }
            return(dt);
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Object> ExecuteScalarAsync(DbCommand command, CancellationToken cancellationToken)
        {
            Object          o         = null;
            ConnectionState state     = this.ConnectionState;
            DbCommand       cm        = command;
            DateTimeOffset? startTime = null;
            DateTimeOffset? endTime   = null;
            Object          ec        = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteScalar, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(null);
                }
                ec = e.ExecutionContext;

                await this.OpenAsync();

                cm.Connection  = this.Connection;
                cm.Transaction = this.Transaction;
                startTime      = DateTimeOffset.Now;
                o = await cm.ExecuteScalarAsync(cancellationToken);

                endTime = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteScalar, this.ConnectionString, ex, ec, cm));
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteScalar, this.ConnectionString
                                                                        , startTime.Value, endTime.Value, ec, cm));
            }
            return(o);
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="script"></param>
        /// <returns></returns>
        public Int32 ExecuteCommand(MySqlScript script)
        {
            var             affectRecordNumber = Int32.MinValue;
            ConnectionState state     = this.ConnectionState;
            DateTimeOffset? startTime = null;
            DateTimeOffset? endTime   = null;
            Object          ec        = null;

            try
            {
                var e = Database.OnCommandExecuting(new MySqlScriptExecutingEventArgs(this.ConnectionString, script));
                if (e != null && e.Cancel == true)
                {
                    return(-1);
                }
                ec = e.ExecutionContext;

                this.Open();
                script.Connection  = this.Connection as MySqlConnection;
                startTime          = DateTimeOffset.Now;
                affectRecordNumber = script.Execute();
                endTime            = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new MySqlScriptErrorEventArgs(this.ConnectionString, ex, ec, script));
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new MySqlScriptExecutedEventArgs(this.ConnectionString
                                                                            , startTime.Value, endTime.Value, ec, script));
            }
            return(affectRecordNumber);
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="connectionAutoClose"></param>
        /// <returns></returns>
        public Int32 ExecuteCommand(DbCommand command)
        {
            var             affectRecordNumber = Int32.MinValue;
            ConnectionState state     = this.ConnectionState;
            DbCommand       cm        = command;
            DateTimeOffset? startTime = null;
            DateTimeOffset? endTime   = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteCommand, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(-1);
                }

                this.Open();
                cm.Connection      = this.Connection;
                cm.Transaction     = this.Transaction;
                startTime          = DateTimeOffset.Now;
                affectRecordNumber = cm.ExecuteNonQuery();
                endTime            = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteCommand, this.ConnectionString, ex, cm));
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteCommand, this.ConnectionString, startTime.Value, endTime.Value, cm));
            }
            return(affectRecordNumber);
        }
示例#9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Object ExecuteScalar(DbCommand command)
        {
            Object          o         = null;
            ConnectionState state     = this.ConnectionState;
            DbCommand       cm        = command;
            DateTimeOffset? startTime = null;
            DateTimeOffset? endTime   = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteScalar, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(null);
                }

                this.Open();
                cm.Connection  = this.Connection;
                cm.Transaction = this.Transaction;
                startTime      = DateTimeOffset.Now;
                o       = cm.ExecuteScalar();
                endTime = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteScalar, this.ConnectionString, ex, cm));
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteScalar, this.ConnectionString, startTime.Value, endTime.Value, cm));
            }
            return(o);
        }
示例#10
0
        public async Task <DbDataReader> ExecuteReaderAsync(DbCommand command, CommandBehavior commandBehavior, CancellationToken cancellationToken)
        {
            DbDataReader   dr        = null;
            DbCommand      cm        = command;
            DateTimeOffset?startTime = null;
            DateTimeOffset?endTime   = null;
            Object         ec        = null;

            try
            {
                var e = Database.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.ExecuteReader, this.ConnectionString, cm));
                if (e != null && e.Cancel == true)
                {
                    return(null);
                }
                ec = e.ExecutionContext;

                await this.OpenAsync();

                cm.Connection  = this.Connection;
                cm.Transaction = this.Transaction;
                startTime      = DateTimeOffset.Now;
                dr             = await cm.ExecuteReaderAsync(commandBehavior, cancellationToken).ConfigureAwait(false);

                endTime = DateTimeOffset.Now;
            }
            catch (Exception ex)
            {
                this.Close();
                this.CatchException(new CommandErrorEventArgs(MethodName.ExecuteReader, this.ConnectionString, ex, ec, cm));
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                Database.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.ExecuteReader, this.ConnectionString
                                                                        , startTime.Value, endTime.Value, ec, cm));
            }
            return(dr);
        }