public Gin GetGinById(int id) { Gin gin = null; using (SqlConnection con = new SqlConnection(this.ConnectionString)) { con.Open(); using (SqlCommand command = new SqlCommand()) { command.CommandText = "SELECT * FROM Gins WHERE Id = @id"; command.Connection = con; command.Parameters.AddWithValue("@id", id); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { gin = new Gin(); gin.ID = int.Parse(reader["ID"].ToString()); gin.Name = (reader["Description"] != DBNull.Value ? reader["Name"].ToString() : string.Empty); gin.Description = (reader["Description"] != DBNull.Value ? reader["Description"].ToString() : string.Empty); gin.Price = double.Parse(reader["Price"] != DBNull.Value ? reader["Price"].ToString() : "0"); } } } return gin; }
public List<Gin> GetGins() { List<Gin> gins = new List<Gin>(); using (SqlConnection con = new SqlConnection(this.ConnectionString)) { con.Open(); using (SqlCommand command = new SqlCommand()) { command.CommandText = "SELECT * FROM Gins"; command.Connection = con; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Gin gin = new Gin(); gin.ID = int.Parse(reader["ID"].ToString()); gin.Name = (reader["Name"] != DBNull.Value ? reader["Name"].ToString() : string.Empty); gins.Add(gin); } } } return gins; }