以下のコードを試しましたが、mysqlデータベースに追加された行レコードを1つ追加するだけでした。私は3行のレコードを入力しましたが、1行のレコードだけが追加されました。何が問題ですか?複数のレコードをdatagridviewからデータベースに追加する方法C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into salesproductnew(salesid,productname,qty,price,grosstotal)values(@salesid,@productname,@qty,@price,@grosstotal)";
cmd.Parameters.AddWithValue("@salesid", lbinvoice.Text);
cmd.Parameters.AddWithValue("@productname", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@qty", dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@price", dataGridView1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@grosstotal", dataGridView1.Rows[i].Cells[5].Value);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record added .............");
con.Close();
}
try..catchでコードをラップし、例外があるかどうかを確認してください。 –
例外はありません。エラーはありません。 1つの行レコードだけがデータベースに追加されました別のレコードがこの問題を解決してください –
誰かが私を助けますか? –