コード例 #1
0
        public Boolean AlreadyInDatabase(String movie)
        {
            // Method to check to see if movie title is already in the database
            Boolean inDatabase       = false;
            string  connectionString = MovieManagerApplication.GetConnectionString();
            string  sqlCommand       = "SELECT  Id, Title,  Year , Director, Genre, RottenTomatoesScore, TotalEarned  FROM  Movies " + "WHERE Title = @Title";
            string  searchValue      = movie;

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(sqlCommand, connection))
                    {
                        command.Parameters.Add("Title", SqlDbType.VarChar, 50).Value = searchValue;
                        connection.Open();

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                inDatabase = true;
                            }
                        }
                        connection.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Database conection failed. Error {ex.Message}");
            }
            return(inDatabase);
        }
コード例 #2
0
        private void ButtonDelete_Click_1(object sender, EventArgs e)
        {
            // Messagebox to ensure user is correctly deleting item
            var userClose = MessageBox.Show("Are you sure you want to delete this entry from the database?", "Delete Entry?", MessageBoxButtons.YesNo);

            if (userClose == DialogResult.Yes)

            // User input validtion for values of form
            {
                bool ifValid = false;
                if (textBoxMovieTitle.Text != deleteMovie.Title)
                {
                    MessageBox.Show("You cannot change the name of the movie you want to delete. Please change it back.");
                }
                else

                {
                    ifValid = true;
                }

                if (ifValid)
                {
                    // Connecting to SQL database to delete entry
                    string connectionString = MovieManagerApplication.GetConnectionString();
                    string sqlCommand       = "DELETE FROM Movies " + "WHERE Id = @Id";
                    try
                    {
                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            using (SqlCommand command = new SqlCommand(sqlCommand, connection))
                            {
                                command.Parameters.Add("Id", SqlDbType.Int).Value = deleteMovie.Id;
                                connection.Open();
                                command.ExecuteNonQuery();
                                connection.Close();
                                MessageBox.Show("Database has been updated");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Database conection failed. Error {ex.Message}");
                    }

                    deleteMovie.ClearMovieEntry();
                    ClearFields();
                    ButtonDelete.Enabled = false;
                }
            }
        }
コード例 #3
0
        private Boolean AlreadyInDatabase(String movie)
        {
            // Method to check if user entry already exists in database
            Boolean    inDatabase       = false;
            MovieEntry updateEntryCheck = new MovieEntry();
            string     connectionString = MovieManagerApplication.GetConnectionString();
            string     sqlCommand       = "SELECT  Id, Title  FROM  Movies " + "WHERE Title = @Title";
            string     searchValue      = movie;

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(sqlCommand, connection))
                    {
                        command.Parameters.Add("Title", SqlDbType.VarChar, 50).Value = searchValue;
                        connection.Open();

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            reader.Read();
                            updateEntryCheck.Id = reader.GetInt32(0);
                            if (updateEntryCheck.Id != updateMovieEntry.Id)
                            {
                                inDatabase = true;
                            }
                        }
                        connection.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Database conection failed. Error {ex.Message}");
            }
            return(inDatabase);
        }
コード例 #4
0
        private void ButtonFind_Click_1(object sender, EventArgs e)
        {
            // Connecting to SQL database to read in values from user search
            string connectionString = MovieManagerApplication.GetConnectionString();
            string sqlCommand       = "SELECT  Id, Title,  Year , Director, Genre, RottenTomatoesScore, TotalEarned  FROM  Movies " + "WHERE Title = @Title";

            string[] movieGenres = { "null", "Animation", "Action", "Comedy", "Drama", "Horror", "Mystery", "Romance", "Science Fiction", "Western" };
            string   searchValue = textBoxMovieTitle.Text;

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(sqlCommand, connection))
                    {
                        command.Parameters.Add("Title", SqlDbType.VarChar, 50).Value = searchValue;
                        connection.Open();

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                updateMovieEntry.Id       = reader.GetInt32(0);
                                updateMovieEntry.Title    = reader.GetString(1);
                                updateMovieEntry.Year     = reader.GetInt32(2);
                                updateMovieEntry.Director = reader.GetString(3);
                                if (reader.GetInt32(4) > 0 && reader.GetInt32(4) < 9)
                                {
                                    int genreValue = reader.GetInt32(4);
                                    updateMovieEntry.Genre = movieGenres[genreValue];
                                }
                                if (!reader.IsDBNull(5))
                                {
                                    updateMovieEntry.RottenTomatoesScore = reader.GetInt32(5);
                                }
                                if (!reader.IsDBNull(6))
                                {
                                    updateMovieEntry.TotalEarned = reader.GetDecimal(6);
                                }
                                textBoxYear.Enabled     = true;
                                textBoxDirector.Enabled = true;
                                textBoxRTScore.Enabled  = true;
                                textBoxEarnings.Enabled = true;
                                comboBoxGenre.Enabled   = true;
                                ButtonUpdate.Enabled    = true;
                            }
                            else
                            {
                                MessageBox.Show("Your movie was not found in the database.");
                            }
                        }
                        connection.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Database conection failed. Error {ex.Message}");
            }

            // Output of values to form
            textBoxMovieTitle.Text = updateMovieEntry.Title;
            textBoxYear.Text       = updateMovieEntry.Year.ToString();
            textBoxDirector.Text   = updateMovieEntry.Director;
            comboBoxGenre.Text     = updateMovieEntry.Genre;
            textBoxRTScore.Text    = updateMovieEntry.RottenTomatoesScore.ToString();
            textBoxEarnings.Text   = updateMovieEntry.TotalEarned.ToString();
        }