Пример #1
0
        public ICommandResult ExecuteCommand(IDataCommand dataCommand)
        {
            ICommandResult result = null;
            var            timer  = new Stopwatch();

            timer.Start();

            try
            {
                try
                {
                    if (dataCommand.Validate() == false)
                    {
                        return(null);
                    }
                }
                catch (Exception exception)
                {
                    result.AddError(LogType.Error, "Validate exeption", exception);
                }
                if (Connection == null)
                {
                    this.OpenSharedConnection();
                }
                //try
                //{

                var command = this.Connection.CreateCommand();

                command.CommandTimeout = 0;
                command.Connection     = this.Connection;
                if (this.Transaction != null)
                {
                    command.Transaction = this.Transaction;
                }
                switch (dataCommand.CommandType)
                {
                case DataCommandType.Select:
                    result = ExecuteSelectCommand((T)dataCommand, command);
                    break;

                case DataCommandType.Insert:
                    result = ExecuteInsertCommand((T)dataCommand, command);
                    break;

                case DataCommandType.Update:
                    result = ExecuteUpdateCommand((T)dataCommand, command);
                    break;

                case DataCommandType.Delete:
                    result = ExecuteDeleteCommand((T)dataCommand, command);
                    break;

                case DataCommandType.TableCreateUpdate:
                    result = ExecuteTableCreateUpdate((T)dataCommand, command);
                    break;
                }
                result.DataCommand = dataCommand;

                //}
                //catch (SqlException exception)
                //{
                //    result.AddError(LogType.Error, "Sql exeption", exception);
                //}
                //catch (Exception ex)
                //{
                //    result.AddError(LogType.Error, "exeption", ex);
                //}
            }
            catch (Exception ex1)
            {
                this.Errors.Add(new DataError
                {
                    StackTrace = ex1.StackTrace,
                    Exception  = ex1,
                    HasError   = true
                });
            }
            finally
            {
                if (this.Transaction == null)
                {
                    this.Connection.Close();
                }

                timer.Stop();
                if (result == null)
                {
                    result = new CommandResult();
                }
                result.ElapsedMilliseconds = timer.ElapsedMilliseconds;


                result.AddMessage($"Executed {dataCommand.CommandType} in {timer.ElapsedMilliseconds}ms with {result.RecordsAffected} rows affected");
            }
            return(result);
        }