Пример #1
0
        public int SaveChanges()
        {
            // add items
            foreach (DBTable table in _tablesToSave)
            {
                table.AddRange(table._itemsToAdd);
                table._itemsToAdd.Clear();
            }
            _tablesToSave.Clear();

            if (!_commands.Any())
            {
                return(0);
            }

            int total = 0;

            using (IDbConnection connection = _commandSet.Connection)
            {
                connection.ConnectionString = _connectionString;

                connection.Open();
                using (IDbTransaction transaction = connection.BeginTransaction())
                {
                    IDbCommand command = null;
                    try
                    {
                        while (_commands.Any())
                        {
                            command = _commands.Dequeue();

                            command.Connection  = connection;
                            command.Transaction = transaction;
                            total += command.ExecuteNonQuery();
                        }
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();

                        throw SqlExceptionMessage.TransformAndLogMessage(ex, command);
                    }
                }
            }

            return(total);
        }
Пример #2
0
        /// <summary>
        /// Don't forget to close DBReader!!!
        /// </summary>
        public DBReader ExecuteCommand(IDbCommand command)
        {
            IDbConnection connection = CommandSet.Connection;

            connection.ConnectionString = ConnectionString;
            connection.Open();

            command.Connection = connection;
            try
            {
                IDataReader reader = command.ExecuteReader();
                return(new DBReader(connection, reader));
            }
            catch (DbException ex)
            {
                throw SqlExceptionMessage.TransformAndLogMessage(ex, command);
            }
        }
Пример #3
0
        public int ExecuteNonQuery(IDbCommand command)
        {
            using (IDbConnection connection = CommandSet.Connection)
            {
                connection.ConnectionString = ConnectionString;
                connection.Open();

                command.Connection = connection;
                try
                {
                    return(command.ExecuteNonQuery());
                }
                catch (DbException ex)
                {
                    // always throws exception
                    throw SqlExceptionMessage.TransformAndLogMessage(ex, command);
                }
            }
        }