public void bAjouter_Click(object sender, EventArgs e) { bool ok = true; foreach (DataGridViewRow row in this.dataGrid.Rows) { if(row.IsNewRow) ok &= this.AddRecord(row); } if(ok) MessageBox.Show("Rows inserted); } public bool AddRecord(DataGridViewRow row) { bool ok = false; try { if(cnx.State==0) cnx.Open(); string rq = @"INSERT INTO EMPLOYE(numE,nom,prenom,salaire) values(@numE, @nom, @P, @sal)"; SqlCommand cmd = new SqlCommand(rq, cnx); cmd.Parameters.AddWithValue("@numE", row.Cells[0]); cmd.Parameters.AddWithValue("@libelle", row.Cells[1]); cmd.Parameters.AddWithValue("@libelle", row.Cells[2]); cmd.Parameters.AddWithValue("@libelle", row.Cells[3]); int resultat = cmd.ExecuteNonQuery(); ok = (resultat > 0); } catch(SQLException ex) { // Logger l'exception sql } finally { cnx.Close(); } return ok; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questioncmd.Parameters.AddWithValue("@libelle", row.Cells[1]); cmd.Parameters.AddWithValue("@libelle", row.Cells[2]); cmd.Parameters.AddWithValue("@libelle", row.Cells[3]);
cmd.Parameters.AddWithValue("@nom", row.Cells[1]); cmd.Parameters.AddWithValue("@Prenom", row.Cells[2]); cmd.Parameters.AddWithValue("@salaire", row.Cells[3]);
StringBuilder SB = new StringBuilder(); SB.Append("INSERT INTO EMPLOYE(numE,nom,prenom,salaire) values("); SB.Append(row.Cells[0].ToString() + ","); SB.Append(row.Cells[1].ToString() + ","); SB.Append(row.Cells[2].ToString() + ","); SB.Append(row.Cells[3].ToString() + ","); SB.Append(")"); SqlCommand cmd = new SqlCommand(SB.ToString(), cnx); int resultat = cmd.ExecuteNonQuery();