此代码仅适用于第一行和 . 我希望此代码与其余行一起使用:
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());
}