示例#1
0
        private DataTable WithReturn(string sql, object parameters, CountValidationEnum countValidation, int n)
        {
            using (var command = _parameterSetter.GetCommand(OpenConnection, sql, parameters))
            {
                try
                {
                    // No need to open a transaction, only to use an open one if exists
                    // That's why _currentTransaction and not OpenTransaction (unlike the on writes)
                    command.Transaction = _currentTransaction;

                    var dt       = FillDataTable(command);
                    var selected = dt.Rows.Count;

                    switch (countValidation)
                    {
                    case CountValidationEnum.Exactly:
                        if (selected != n)
                        {
                            throw new UnexpectedNumberOfRowsSelectedException(command, selected);
                        }
                        break;

                    case CountValidationEnum.NoLessThan:
                        if (selected < n)
                        {
                            throw new UnexpectedNumberOfRowsSelectedException(command, selected);
                        }
                        break;

                    case CountValidationEnum.NoMoreThan:
                        if (selected > n)
                        {
                            throw new UnexpectedNumberOfRowsSelectedException(command, selected);
                        }
                        break;
                    }

                    CloseIfNeeded();
                    return(dt);
                }
                catch
                {
                    CloseRegardless(true);
                    throw;
                }
            }
        }
示例#2
0
        private void WithoutReturn(string sql, object parameters, CountValidationEnum countValidation, int n)
        {
            using (var command = _parameterSetter.GetCommand(OpenConnection, sql, parameters))
            {
                try
                {
                    command.Transaction = OpenTransaction;

                    var affected = command.ExecuteNonQuery();

                    switch (countValidation)
                    {
                    case CountValidationEnum.Exactly:
                        if (affected != n)
                        {
                            throw new UnexpectedNumberOfRowsAffectedException(command, affected);
                        }
                        break;

                    case CountValidationEnum.NoLessThan:
                        if (affected < n)
                        {
                            throw new UnexpectedNumberOfRowsAffectedException(command, affected);
                        }
                        break;

                    case CountValidationEnum.NoMoreThan:
                        if (affected > n)
                        {
                            throw new UnexpectedNumberOfRowsAffectedException(command, affected);
                        }
                        break;
                    }

                    CloseIfNeeded();
                }
                catch
                {
                    CloseRegardless(true);
                    throw;
                }
            }
        }