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); }
/// <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); } }
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); } } }