public void AddContribution(Contribution c) { GetFromDB db = new GetFromDB(_conStr); Contribution exists = db.GetContributions().Find(contribution => contribution.SimchaId == c.SimchaId && contribution.ContributorId == c.ContributorId); using (SqlConnection connection = new SqlConnection(_conStr)) using (SqlCommand command = connection.CreateCommand()) { if (exists != null) { command.CommandText = "UPDATE Contributions SET Amount = @amount WHERE SimchaId = @simchaId AND ContributorId = @contributorId"; } else { command.CommandText = "INSERT INTO Contributions (Amount, ContributorId, SimchaId) " + " VALUES (@amount, @contributorId, @simchaId)"; } command.Parameters.AddWithValue("@amount", c.Amount); command.Parameters.AddWithValue("@contributorId", c.ContributorId); command.Parameters.AddWithValue("@simchaId", c.SimchaId); connection.Open(); command.ExecuteNonQuery(); } }
//public List<Contributor> GetContributors(string searchTerm) //{ // List<Contributor> result = new List<Contributor>(); // using (SqlConnection connection = new SqlConnection(_conStr)) // using (SqlCommand command = connection.CreateCommand()) // { // command.CommandText = "SELECT * FROM Contributors WHERE FirstName LIKE @searchTerm OR LastName LIKE @searchTerm"; // command.Parameters.AddWithValue("@searchTerm", $"'%{searchTerm}%'"); // connection.Open(); // SqlDataReader reader = command.ExecuteReader(); // while (reader.Read()) // { // Contributor c = new Contributor(); // c.Id = (int)reader["Id"]; // c.FirstName = (string)reader["FirstName"]; // c.LastName = (string)reader["LastName"]; // c.CellNumber = (string)reader["CellNumber"]; // c.DateCreated = (DateTime)reader["DateCreated"]; // c.AlwaysInclude = (bool)reader["AlwaysInclude"]; // result.Add(c); // } // } // return result; //} //private List<Deposit> GetDeposits(int contributorId) //{ // List<Deposit> result = new List<Deposit>(); // using (SqlConnection connection = new SqlConnection(_conStr)) // using (SqlCommand command = connection.CreateCommand()) // { // command.CommandText = "SELECT * FROM Deposits WHERE ContributorId = @id"; // command.Parameters.AddWithValue("@id", contributorId); // connection.Open(); // SqlDataReader reader = command.ExecuteReader(); // while (reader.Read()) // { // Deposit d = new Deposit(); // d.Amount = (decimal)reader["Amount"]; // d.Id = (int)reader["Id"]; // d.ContributorId = (int)reader["ContributorId"]; // result.Add(d); // } // return result; // } //} public List <Contribution> GetContributions() { List <Contribution> result = new List <Contribution>(); using (SqlConnection connection = new SqlConnection(_conStr)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "SELECT * FROM Contributions"; connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Contribution c = new Contribution(); c.Amount = (decimal)reader["Amount"]; c.SimchaId = (int)reader["SimchaId"]; c.ContributorId = (int)reader["ContributorId"]; result.Add(c); } return(result); } }