/// <summary> /// Deletes the team. Players on the team will be listed as free agents. /// </summary> /// <param name="team">The team.</param> public void DeleteTeam(Team team) { int teamId = team.Id; if (team != null) { foreach (Player p in team.Players) { p.TeamId = 1; _DbPlayer.EditPlayer(p); } } TransactionOptions to = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, to)) { using (SqlConnection connection = DBConnection.GetSqlConnection()) { connection.Open(); using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "DELETE FROM Team WHERE Id = @id; DBCC CHECKIDENT (@tableName, RESEED);"; command.Parameters.AddWithValue("id", teamId); command.Parameters.AddWithValue("tableName", "Team"); command.ExecuteNonQuery(); } connection.Close(); } scope.Complete(); } }
/// <summary> /// Persists Team. /// </summary> /// <param name="team">The team.</param> /// <returns>TeamId given to the persisted team</returns> public int CreateTeam(Team team) { _DbPlayer = new DBPlayer(); TransactionOptions to = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, to)) { using (SqlConnection connection = DBConnection.GetSqlConnection()) { connection.Open(); using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "INSERT INTO Team (Name) OUTPUT INSERTED.ID VALUES(@Name)"; command.Parameters.AddWithValue("Name", team.Name); team.Id = (int)command.ExecuteScalar(); if (team != null) { if (team.Players != null) { foreach (Player p in team.Players) { _DbPlayer.EditPlayer(p); } } } } connection.Close(); } scope.Complete(); } return(team.Id); }