private void ajouterUneCatégorieToolStripMenuItem_Click(object sender, EventArgs e) { V_ajouter_C fenetre; fenetre = new V_ajouter_C(); fenetre.MdiParent = this; fenetre.Show(); }
public static void ajouterCategorie(Categories uneCategorie) { try { M_connexion.Gestion.Open(); string requete = "SELECT count(*) FROM Categories WHERE libelleCategorie = ?"; MySqlCommand maCommande = new MySqlCommand(requete, M_connexion.Gestion); MySqlParameter Param1 = maCommande.Parameters.Add("@libelleCategorie", MySqlDbType.VarChar); Param1.Value = uneCategorie.LibelleCategorie; int nbCategories = Convert.ToInt16(maCommande.ExecuteScalar()); M_connexion.Gestion.Close(); if (nbCategories <= 0) { try { // Ouverture de la connexion M_connexion.Gestion.Open(); // Requête SQL string reqSQL = "INSERT INTO Categories 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("@idFournisseurs", MySqlDbType.Int16); MySqlParameter Para1 = Command1.Parameters.Add("@libelleCategorie", MySqlDbType.VarChar); // Affectation des valeurs // On abandonne le faux paramètre 1 car l'ID est auto-incrémenté Para1.Value = uneCategorie.LibelleCategorie; Command1.ExecuteNonQuery(); M_connexion.Gestion.Close(); MessageBox.Show("Catégorie ajouté."); // Fermeture de la fenêtre V_ajouter_C fenetre = new V_ajouter_C(); fenetre.Close(); } catch (Exception ex) { MessageBox.Show("Erreur :" + ex.Message); } } else { throw new Exception("La catégorie existe déjà."); } } catch (Exception ex) { MessageBox.Show("Erreur :" + ex.Message); } }