//Inserting data into Database public bool Insert(moviesClass m) { //Creating a default return type and setting value to false bool isSuccess = false; //Step 1: Connect Database SqlConnection conn = new SqlConnection(myconnstrng); //Check Database Connection m.Checkconnection(conn); try { //Step 2: Create SQL Query to insert Data string sql = "INSERT INTO Movies (MovieName, MovieGenre, IMDBRating, Description, Platform) VALUES (@MovieName, @MovieGenre, @IMDBRating, @Description, @Platform)"; //Creating SQL Command SqlCommand cmd = new SqlCommand(sql, conn); //Creating Paramaters to add data cmd.Parameters.AddWithValue("@MovieName", m.MovieName); cmd.Parameters.AddWithValue("@MovieGenre", m.MovieGenre); cmd.Parameters.AddWithValue("@IMDBRating", m.IMDBRating); cmd.Parameters.AddWithValue("@Description", m.Description); cmd.Parameters.AddWithValue("@Platform", m.Platform); //Open connection to Database conn.Open(); int rows = cmd.ExecuteNonQuery(); //if the query runs successfully then the value of rows will be greater than 0 and less than 0 otherwise if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch { } finally { conn.Close(); } return(isSuccess); }
//Method to delete Data from Database public bool Delete(moviesClass m) { //Create a default return value bool isSuccess = false; //Create SQL Connection SqlConnection conn = new SqlConnection(myconnstrng); m.Checkconnection(conn); try { //SQL to delete Data string sql = "DELETE FROM Movies WHERE MovieID=@MovieID"; //Create SQL Command SqlCommand cmd = new SqlCommand(sql, conn); //Create MovieID Parameter for deletion cmd.Parameters.AddWithValue("@MovieID", m.MovieID); //Open Database Connection conn.Open(); int rows = cmd.ExecuteNonQuery(); //if the query runs successfully then the value of rows will be greater than 0 and less than 0 otherwise if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch { } finally { //close connection conn.Close(); } return(isSuccess); }