private void AffecterValeurs(Materiel unMateriel) { // On affecte les valeurs aux champs en fonction du clique dans le menu déroulant de produits Materiel leMateriel = unMateriel; idMaterielCourant = leMateriel.getId(); t_nom.Text = leMateriel.getLibelle(); t_ht.Value = leMateriel.getPrixAch(); t_description.Text = leMateriel.getDescription(); t_marque.Text = leMateriel.getMarque(); t_modele.Text = leMateriel.getModele(); t_fournisseur.Text = leMateriel.getFournisseur(); }
// Fonction d'ajout de matériel public static void ajouterMateriel(Materiel unMateriel) { try { // Ouverture de la connexion M_Connexion.Gestion.Open(); // Requête SQL string reqSQL = "INSERT INTO materiel VALUES (NULL,?,?,?,?,?,?,?,?,?)"; // Execution de la requête MySqlCommand Command1 = new MySqlCommand(reqSQL, M_Connexion.Gestion); // Création des paramètres correspondants aux ? //MySqlParameter Param1 = Command1.Parameters.Add("@idMateriel", MySqlDbType.Int16); MySqlParameter Param2 = Command1.Parameters.Add("@libelle", MySqlDbType.VarChar); MySqlParameter Param3 = Command1.Parameters.Add("@description", MySqlDbType.VarChar); MySqlParameter Param4 = Command1.Parameters.Add("@prixAchHT", MySqlDbType.Decimal); MySqlParameter Param6 = Command1.Parameters.Add("@iSousCategorie", MySqlDbType.Int16); MySqlParameter Param7 = Command1.Parameters.Add("@Marque", MySqlDbType.VarChar); MySqlParameter Param8 = Command1.Parameters.Add("@Modele", MySqlDbType.VarChar); MySqlParameter Param9 = Command1.Parameters.Add("@Fournisseur", MySqlDbType.VarChar); MySqlParameter Param10 = Command1.Parameters.Add("@nbstock", MySqlDbType.Int16); MySqlParameter Param11 = Command1.Parameters.Add("@cheminImage", MySqlDbType.VarChar); // Affectation des valeurs // On abandonne le faux paramètre 1 car l'ID est auto-incrémenté Param2.Value = unMateriel.getLibelle(); Param3.Value = unMateriel.getDescription(); Param4.Value = unMateriel.getPrixAch(); Param6.Value = unMateriel.getIdSousCategorie(); Param7.Value = unMateriel.getMarque(); Param8.Value = unMateriel.getModele(); Param9.Value = unMateriel.getFournisseur(); Param10.Value = unMateriel.getNbStock(); Param11.Value = unMateriel.getCheminImage(); Command1.ExecuteNonQuery(); M_Connexion.Gestion.Close(); MessageBox.Show("Matériel ajouté."); } catch (Exception ex) { MessageBox.Show("Erreur :" + ex.Message); M_Connexion.Gestion.Close(); } }
public static void SupprimerMateriel(Materiel unMateriel) { try { // Ouverture de la connexion M_Connexion.Gestion.Open(); // Requête SQL string reqSQL = "DELETE FROM materiel WHERE idMateriel = ?"; // Execution de la requête MySqlCommand Command1 = new MySqlCommand(reqSQL, M_Connexion.Gestion); // Création des paramètres correspondants aux ? MySqlParameter Param1 = Command1.Parameters.Add("@idMateriel", MySqlDbType.Int16); // Affectation des valeurs Param1.Value = unMateriel.getId(); Command1.ExecuteNonQuery(); M_Connexion.Gestion.Close(); MessageBox.Show("Matériel supprimé."); } catch (Exception ex) { MessageBox.Show("Erreur :" + ex.Message); M_Connexion.Gestion.Close(); } }
// Fonction d'ajout de matériel public static void modifierMateriel(Materiel unMateriel) { try { // Ouverture de la connexion M_Connexion.Gestion.Open(); // Requête SQL string reqSQL = "UPDATE materiel SET libelle = ?, description = ?, prixAchHT = ?, idSousCategorie = ?, Marque = ?, Modele = ?, Fournisseur = ? WHERE idMateriel = ?"; // Execution de la requête MySqlCommand Command1 = new MySqlCommand(reqSQL, M_Connexion.Gestion); // Création des paramètres correspondants aux ? //MySqlParameter Param1 = Command1.Parameters.Add("@idMateriel", MySqlDbType.Int16); MySqlParameter Param1 = Command1.Parameters.Add("@libelle", MySqlDbType.VarChar); MySqlParameter Param2 = Command1.Parameters.Add("@description", MySqlDbType.VarChar); MySqlParameter Param3 = Command1.Parameters.Add("@prixAchHT", MySqlDbType.Decimal); MySqlParameter Param5 = Command1.Parameters.Add("@iSousCategorie", MySqlDbType.Int16); MySqlParameter Param6 = Command1.Parameters.Add("@Marque", MySqlDbType.VarChar); MySqlParameter Param7 = Command1.Parameters.Add("@Modele", MySqlDbType.VarChar); MySqlParameter Param8 = Command1.Parameters.Add("@Fournisseur", MySqlDbType.VarChar); MySqlParameter Param9 = Command1.Parameters.Add("@idMateriel", MySqlDbType.Int16); // Affectation des valeurs // Le param1 est auto-incrémenté Param1.Value = unMateriel.getLibelle(); Param2.Value = unMateriel.getDescription(); Param3.Value = unMateriel.getPrixAch(); Param5.Value = unMateriel.getIdSousCategorie(); Param6.Value = unMateriel.getMarque(); Param7.Value = unMateriel.getModele(); Param8.Value = unMateriel.getFournisseur(); Param9.Value = unMateriel.getId(); Command1.ExecuteNonQuery(); M_Connexion.Gestion.Close(); MessageBox.Show("Matériel modifié."); } catch (Exception ex) { MessageBox.Show("Erreur :" + ex.Message); M_Connexion.Gestion.Close(); } }
public static Collection<Materiel> GetMateriel() { Collection<Materiel> CollectionMateriel = new Collection<Materiel>(); try { CollectionMateriel.Clear(); // Ouverture de la connexion M_Connexion.Gestion.Open() ; // Requête SQL String ReqSQL = "SELECT * FROM materiel"; // Déclaration du curseur et execution de la requête MySqlDataReader MonReaderMateriel; MySqlCommand Command1 = new MySqlCommand(ReqSQL, M_Connexion.Gestion); MonReaderMateriel = Command1.ExecuteReader(); Materiel nouveauMateriel; while (MonReaderMateriel.Read()) { nouveauMateriel = new Materiel(int.Parse(MonReaderMateriel[0].ToString()), MonReaderMateriel[1].ToString(), MonReaderMateriel[2].ToString(), decimal.Parse(MonReaderMateriel[3].ToString()), int.Parse(MonReaderMateriel[4].ToString()), MonReaderMateriel[5].ToString(), MonReaderMateriel[6].ToString(), MonReaderMateriel[7].ToString(), int.Parse(MonReaderMateriel[8].ToString()), MonReaderMateriel[9].ToString()); CollectionMateriel.Add(nouveauMateriel); } // Fermeture de la connexion M_Connexion.Gestion.Close(); } catch (Exception ex) { MessageBox.Show("Erreur :" + ex.Message); M_Connexion.Gestion.Close(); } return CollectionMateriel; }
private void b_validerModification_Click(object sender, EventArgs e) { try { Materiel unMateriel = null; unMateriel = new Materiel(idMaterielCourant, t_nom.Text, t_description.Text, t_ht.Value, CollectionSousCategories[list_souscategories.SelectedIndex].getCode(), t_marque.Text, t_modele.Text, t_fournisseur.Text, 0, ""); M_Materiel.modifierMateriel(unMateriel); } catch (Exception ex) { MessageBox.Show("Veuillez selectionner un matériel."); } }
private void b_validerAjout_Click(object sender, EventArgs e) { try { string filename = open.FileName; FileInfo objFile = new FileInfo(filename); // On créé un objet de matériel qui sera utilisé pour l'ajout de celui-ci dans la BDD Materiel unMateriel = null; int idMateriel = 1; unMateriel = new Materiel(idMateriel, t_nom.Text, t_description.Text, t_ht.Value, CollectionSousCategories[list_souscategories.SelectedIndex].getCode(), t_marque.Text, t_modele.Text, t_fournisseur.Text, 0, "content/images/products/" + fichierSansExt); // On ajoute le matériel M_Materiel.ajouterMateriel(unMateriel); // On ajout la photo ajouterPhoto(open); } catch (Exception ex) { MessageBox.Show("Veuillez compléter tous les champs."); } // On rebloque les boutons et on recharge la liste BloquerChampsSaisie(); RemiseAZero(); list_materiels.Enabled = true; b_ajouterMateriel.Enabled = true; b_modifierMateriel.Enabled = true; b_supprimerMateriel.Enabled = true; // Vide les items et les re-remplis -> Ne fonctionne pas list_materiels.Items.Clear(); RemplirComboMateriel(); }