public int AddSimcha(Simchas simcha) { using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO Simcha (Name, Date)VALUES (@name, @date) SELECT @@IDENTITY"; command.Parameters.AddWithValue("@name", simcha.Name); command.Parameters.AddWithValue("@date", simcha.Date); connection.Open(); return (int)(decimal)command.ExecuteScalar(); } }
public void AddSimcha(Simchas simcha) { var connection = new SqlConnection(_connectionString); var cmd = connection.CreateCommand(); connection.Open(); cmd.CommandText = @"INSERT INTO Simchas (Name, Date) VALUES (@name, @date) SELECT SCOPE_IDENTITY()"; cmd.Parameters.AddWithValue("@name", simcha.Name); cmd.Parameters.AddWithValue("@date", simcha.Date); simcha.Id = (int)cmd.ExecuteScalar(); }
public IEnumerable <Simchas> GetAllSimchas() { var simchas = new List <Simchas>(); var connection = new SqlConnection(_connectionString); var cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Simchas"; connection.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { var simcha = new Simchas(); simcha.Id = (int)reader["Id"]; simcha.Name = (string)reader["Name"]; simcha.Date = (DateTime)reader["Date"]; simcha.ContributorAmount = (int)reader["ContributorAmount"]; simcha.Total = (int)reader["Total"]; simchas.Add(simcha); } return(simchas); }
public Simchas GetSimchaById(int simchaId) { var connection = new SqlConnection(_connectionString); var cmd = connection.CreateCommand(); connection.Open(); cmd.CommandText = "SELECT * FROM Simchas WHERE Id = @id"; cmd.Parameters.AddWithValue("@id", simchaId); var reader = cmd.ExecuteReader(); if (!reader.Read()) { return(null); } var simcha = new Simchas(); simcha.Id = (int)reader["Id"]; simcha.Name = (string)reader["Name"]; simcha.Date = (DateTime)reader["Date"]; simcha.ContributorAmount = (int)reader["ContributorAmount"]; simcha.Total = (int)reader["Total"]; return(simcha); }
public Simchas GetASimchas(int? id) { Simchas simchas = new Simchas(); using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Simcha"; if (id != null) { command.CommandText += " WHERE Id = @id"; command.Parameters.AddWithValue("@id", id); } command.CommandText += " ORDER BY Id DESC"; connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { simchas.Id = (int)reader["id"]; simchas.Name = (string)reader["Name"]; simchas.Date = (DateTime)reader["Date"]; } } return simchas; }