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
| private void btnUpdate_Click(object sender, EventArgs e)
{
if (cbUpdate.Text != "")
{
string s = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}";
OleDbConnection conn = new OleDbConnection(String.Format(s, this.pathDB));
using (conn)
{
try
{
using (OleDbCommand cmd = new OleDbCommand())
{
//cmd.CommandText = "update Personnes set Nom = 'A', Prenom = 'B' where Num = 1"; // ok fonctionne
cmd.CommandText = "update Personnes set Nom = @nom, Prenom = @prenom where Num = @num";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@num", cbUpdate.Text);
cmd.Parameters.AddWithValue("@nom", tbUpdateNom.Text);
cmd.Parameters.AddWithValue("@prenom", tbUpdatePrenom.Text);
conn.Open();
int nb = cmd.ExecuteNonQuery();
MessageBox.Show(String.Format("{0} enregistrements modifiés", nb));
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("bad ! : " + ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} |