//Method for deleting from database public bool Delete(Boardgame p) { using (SqlConnection con = new SqlConnection(DB.connectionString)) { bool isSuccess = false; try { string sql = "DELETE FROM Game_Library WHERE Boardgame_Id = @Boardgame_Id"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("Boardgame_Id", p.boardgameId); con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception) { throw; } return(isSuccess); } }
//Method to insert to the database public bool Insert(Boardgame p) { using (SqlConnection con = new SqlConnection(DB.connectionString)) { bool isSuccess = false; try { //Query to insert to the database string sql = "INSERT INTO Game_Library (Boardgame_Name, Player_Count, Audience, Game_Time, Distributor, GameTag) VALUES (@Boardgame_Name, @Player_Count, @Audience, @Game_Time, @Distributor, @GameTag)"; SqlCommand cmd = new SqlCommand(sql, con); //Parameter to add data cmd.Parameters.AddWithValue("@Boardgame_Name, ", p.boardgameName); cmd.Parameters.AddWithValue("@Player_Count, ", p.playerCount); cmd.Parameters.AddWithValue("@Audience, ", p.audience); cmd.Parameters.AddWithValue("@Game_Time, ", p.gameTime); cmd.Parameters.AddWithValue("@Distributor, ", p.distributor); cmd.Parameters.AddWithValue("@GameTag, ", p.gameTag); con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception) { throw; } finally { con.Close(); } return(isSuccess); } }
//Method to update the database public bool Update(Boardgame p) { using (SqlConnection con = new SqlConnection(DB.connectionString)) { bool isSuccess = false; try { string sql = "UPDATE Game_Library SET Boardgame_Name = @Boardgame_Name, Player_Count = @Player_Count, Audience = @Audience, Game_Time = @Game_Time, Distributor = @Distributor, GameTag = @GameTag WHERE Boardgame_Id = @Boardgame_Id"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("@Boardgame_Name, ", p.boardgameName); cmd.Parameters.AddWithValue("@Player_Count, ", p.playerCount); cmd.Parameters.AddWithValue("@Audience, ", p.audience); cmd.Parameters.AddWithValue("@Game_Time, ", p.gameTime); cmd.Parameters.AddWithValue("@Distributor, ", p.distributor); cmd.Parameters.AddWithValue("@GameTag, ", p.gameTag); con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception) { throw; } finally { con.Close(); } return(isSuccess); } }
private void btnInsert_Click(object sender, RoutedEventArgs e) { Domain.Boardgame p = new Domain.Boardgame { boardgameName = txtBrætspil.Text, playerCount = txtAntal.Text, audience = txtAldersgruppe.Text, gameTime = txtSpilletid.Text, distributor = txtDistrubutør.Text, gameTag = txtGenre.Text }; bool success = p.Insert(p); if (success == true) { MessageBox.Show("Nyt brætspil er tilføjet"); } else { MessageBox.Show("Brætspil blev ikke tilføjet"); } }