// Méthode pour ajouter un contact public void AddContact(imagController contact) { try { // Ouverture de la connexion SQL this.connection.Open(); // Création d'une commande SQL en fonction de l'objet connection MySqlCommand cmd = this.connection.CreateCommand(); // Requête SQL cmd.CommandText = "INSERT INTO contact (idimg, longueu, hautueur) VALUES (@idimg, @longueur, @hautueur)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@idimg", contact.idimg); cmd.Parameters.AddWithValue("@longueur", contact.longueur); cmd.Parameters.AddWithValue("@hautueur", contact.hautueur); // Exécution de la commande SQL cmd.ExecuteNonQuery(); // Fermeture de la connexion this.connection.Close(); } catch { // Gestion des erreurs : // Possibilité de créer un Logger pour les exceptions SQL reçus // Possibilité de créer une méthode avec un booléan en retour pour savoir si le contact à été ajouté correctement. } }
protected void Page_Load(object sender, EventArgs e) { fonter(); if ((!SessionModel.Connecté) || (SessionModel.SessionID != Session.SessionID)) { SessionModel.Reset(); Response.Redirect("index.aspx"); } String civ, pren, nom, soc; civ = SessionModel.Civilité; pren = SessionModel.PrénomUser; nom = SessionModel.NomUser; soc = SessionModel.Société; infoUtilisateur.Text = "BIENVENUE " + civ + ((pren == "" || pren.ToUpper() == "NULL") ? "" : (" " + pren)) + " " + nom + ((soc == "" || soc.ToUpper() == "NULL") ? "" : (" / " + soc)); imagController imagC = new imagController(); imagC.idimg = "jo"; imagC.longueur = 3; imagC.hautueur = 2; load(); // Création de l'objet Bdd pour l'intéraction avec la base de donnée MySQL nbLettreTxt.Visible = false; ImagModel bdd = new ImagModel(); bdd.AddContact(imagC); ReadOnly(); }
static void Main(string[] args) { // Créer d'un contact à ajouter imagController imagC = new imagController(); imagC.idimg = "jo"; imagC.longueur = 3; imagC.hautueur = 2; // Création de l'objet Bdd pour l'intéraction avec la base de donnée MySQL ImagModel bdd = new ImagModel(); bdd.AddContact(imagC); }