Insert ou update sur table mysql
Bonjour,
J'aimerais que mon application sache a qu'elle moment il doit ecrire ou modifier mes champs dans ma table de donnée
j'ai essayé plusieur chose mais je n'y arrive pas.
j'ai trouvé ceci sur le net
Je n'arrive pas a l'interpréter
je mets un morceau de mon code
Pouvez vous m'aider svp?
Merci a tous
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
using (wConnexion)
{
// Instancier lobjet Command
using (MySqlCommand wCommand = wConnexion.CreateCommand())
{
// UPDATE de la requette
wCommand.CommandText = "UPDATE `test` SET nom=@nom, prenom=@prenom, message=@message, sexe=@sexe";
wCommand.Parameters.AddWithValue("@nom", textBox2.Text);
wCommand.Parameters.AddWithValue("@prenom", textBox3.Text);
wCommand.Parameters.AddWithValue("@message", textBox4.Text);
wCommand.Parameters.AddWithValue("@sexe", comboBox2.Text);
// Insert de la requête
wCommand.CommandText = "INSERT INTO test (nom, prenom, message, sexe) VALUES (@nom, @prenom, @message, @sexe)";
wCommand.Parameters.AddWithValue("@nom", textBox2.Text);
wCommand.Parameters.AddWithValue("@prenom", textBox3.Text);
wCommand.Parameters.AddWithValue("@message", textBox4.Text);
wCommand.Parameters.AddWithValue("@sexe", comboBox2.Text);
// Exécution de la requête
try
{
wCommand.ExecuteNonQuery();
MessageBox.Show("Enregistrement effectué!");
}
catch (InvalidOperationException)
{
MessageBox.Show("Erreur impossible d'écrire dans la table!");
} |