該代碼可以使用第一行只有 和。我想這個代碼與行的其餘工作:插入多條記錄從DataGridView
private void Save_Click(object sender, EventArgs e)
{
textBox1.Text =orderdetailsDataGridView.Rows.Count.ToString();
try
{
using (SqlCommand cmd = new SqlCommand())
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "OD1";
foreach (DataGridViewRow row in orderdetailsDataGridView.Rows)
{
if (!row.IsNewRow)
{
for (int x = 0; x < orderdetailsDataGridView.Rows.Count; x++)
{
cmd.Parameters.Add("@order_id", SqlDbType.Int).Value = row.Cells[0].Value;
cmd.Parameters.Add("@prod_id", SqlDbType.Int).Value = row.Cells[1].Value;
cmd.Parameters.Add("@qun", SqlDbType.Int).Value = row.Cells[2].Value;
cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = row.Cells[3].Value;
cmd.ExecuteNonQuery();
x++;
}
}
}
}
}
catch (Exception er)
{
MessageBox.Show(er.ToString());
}
爲什麼你在做'X ++'兩次循環? – Habib