public static MovieRecord GetByID(int id) { using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString)) { conn.Open(); using (var command = new NpgsqlCommand("SELECT * FROM movies WHERE movie_id = @ID", conn)) { command.Parameters.AddWithValue("@ID", id); NpgsqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); double pp = Convert.ToDouble(reader["price"]); var copies = CopyRecord.GetByMovieId(id); return(new MovieRecord(id, (string)reader["title"], (int)reader["year"], pp, copies)); } else { return(null); } } } }
public List <MovieRecord> GetAllMovies() { using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString)) { conn.Open(); using (var command = new NpgsqlCommand("SELECT * FROM movies", conn)) { NpgsqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { double pp = Convert.ToDouble(reader["price"]); int id = (int)reader["movie_id"]; var copies = CopyRecord.GetByMovieId(id); Movies.Add(new MovieRecord(id, (string)reader["title"], (int)reader["year"], pp, copies)); } } } return(Movies); } }