public static bool AddNewMovie(Movie movie) { const string QUARY = @"Exec dbo.AddNewMovie @ID_Record, @PlayTyme, @Genre, @Quality"; var connectionString = GetConnectionString(); using (var connection = new SqlConnection(connectionString)) { using (var command = new SqlCommand(QUARY, connection)) { command.Parameters.Add("@ID_Record", SqlDbType.Int); command.Parameters["@ID_Record"].Value = movie.IDRecord; command.Parameters.Add("@PlayTyme", SqlDbType.NVarChar); command.Parameters["@PlayTyme"].Value = movie.PlayTime; command.Parameters.Add("@Genre", SqlDbType.NVarChar); command.Parameters["@Genre"].Value = movie.Genre; command.Parameters.Add("@Quality", SqlDbType.NVarChar); command.Parameters["@Quality"].Value = movie.Quality; command.Connection.Open(); int count; try { count = command.ExecuteNonQuery(); } catch (InvalidCastException) { return false; } if (count == 1) { return true; } return false; } } }
public static void AddNewMovie(Movie movie) { RecordAccess.AddNewMovie(movie); }
public static Movie GetMovieById(int id) { const string QUARY = @"Exec dbo.GetMovieById @ID_Record"; var connectionString = GetConnectionString(); using (var connection = new SqlConnection(connectionString)) { using (var command = new SqlCommand(QUARY, connection)) { command.Parameters.Add("@ID_Record", SqlDbType.Int); command.Parameters["@ID_Record"].Value = id; command.Connection.Open(); Movie result = null; using (var reader = command.ExecuteReader()) { while (reader.Read()) { result = new Movie { IDRecord = (int)reader["ID_Record"], Genre = reader["Genre"].ToString(), PlayTime = reader["PlayTime"].ToString(), Quality = reader["Quality"].ToString() }; } reader.Close(); } return result; } } }