我試過下面的代碼,但我可以添加一行記錄只添加到mysql數據庫。我輸入了三行記錄,但只添加了一行記錄。問題是什麼?如何添加多個記錄添加到數據庫從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包裝代碼,看看你是否有例外 –
我也沒有例外。沒有錯誤。只有一行記錄只在數據庫中添加了什麼發生了另一個記錄請修復這個問題 –
任何一個幫助我? –