我只能更新1行但是当我更新它时两行或多行所有值都是相同的 我不知道将sqlcommand置于循环中的错误是什么
string date = dt_date.Value.ToString("yyyy-MM-dd");
SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["POSdb"].ConnectionString);
string strQry = "UPDATE CounterChecking " +
"SET part_no = @c0,po_no = @po_no,invoice = @invoice,part_name = @c1," +
"date = @date,color = @c2,qty_ordered = @c3,qty_for_delivery = @c4,qty_recieved = @c5," +
"variance = @c6,unit_price = @c7, total = @c8, remarks = @c9 " +
"WHERE po_no= '" + cb_po_search.Text + "'";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
sc.Open();
SqlCommand scmd = new SqlCommand(strQry, sc);
scmd.Parameters.AddWithValue("@c0", row.Cells[0].Value);
scmd.Parameters.Add("@po_no", SqlDbType.VarChar).Value = tb_ponumber2.Text;
scmd.Parameters.Add("@invoice", SqlDbType.VarChar).Value = tb_invoice2.Text;
scmd.Parameters.AddWithValue("@c1", row.Cells[1].Value);
scmd.Parameters.Add("@date", SqlDbType.Date).Value = date;
scmd.Parameters.AddWithValue("@c2", row.Cells[2].Value);
scmd.Parameters.AddWithValue("@c3", row.Cells[3].Value);
scmd.Parameters.AddWithValue("@c4", row.Cells[4].Value);
scmd.Parameters.AddWithValue("@c5", row.Cells[5].Value);
scmd.Parameters.AddWithValue("@c6", row.Cells[6].Value);
scmd.Parameters.AddWithValue("@c7", row.Cells[7].Value);
scmd.Parameters.AddWithValue("@c8", row.Cells[8].Value);
scmd.Parameters.AddWithValue("@c9", row.Cells[9].Value);
scmd.ExecuteNonQuery();
scmd.Dispose();
sc.Close();
MessageBox.Show("Successfully Updated!");}
sc.Close();
}
没有错误,但它每行给我所有相同的数据
答案 0 :(得分:1)
初始化循环内的SqlCommand
并在执行命令后处理对象。尝试使用try catch finally
块最终处理命令对象。
SqlCommand scmd;
do
{
scmd = new SqlCommand(strQry, sc);
scmd.Parameters.AddWithValue("@c0", dataGridView1.Rows[counter].Cells[0].Value);
scmd.Parameters.Add("@po_no", SqlDbType.VarChar).Value = tb_ponumber2.Text;
scmd.Parameters.Add("@invoice", SqlDbType.VarChar).Value = tb_invoice2.Text;
scmd.Parameters.AddWithValue("@c1", dataGridView1.Rows[counter].Cells[1].Value);
scmd.Parameters.Add("@date", SqlDbType.Date).Value = date;
scmd.Parameters.AddWithValue("@c2", dataGridView1.Rows[counter].Cells[2].Value);
scmd.Parameters.AddWithValue("@c3", dataGridView1.Rows[counter].Cells[3].Value);
scmd.Parameters.AddWithValue("@c4", dataGridView1.Rows[counter].Cells[4].Value);
scmd.Parameters.AddWithValue("@c5", dataGridView1.Rows[counter].Cells[5].Value);
scmd.Parameters.AddWithValue("@c6", dataGridView1.Rows[counter].Cells[6].Value);
scmd.Parameters.AddWithValue("@c7", dataGridView1.Rows[counter].Cells[7].Value);
scmd.Parameters.AddWithValue("@c8", dataGridView1.Rows[counter].Cells[8].Value);
scmd.Parameters.AddWithValue("@c9", dataGridView1.Rows[counter].Cells[9].Value);
counter++;
scmd.ExecuteNonQuery();
scmd.Dispose();
MessageBox.Show("Successfully Updated!");
} while (counter < dataGridView1.Rows.Count);
答案 1 :(得分:0)
您需要为每次迭代重新创建命令。如果你在do循环中移动SqlCommand scmd = new SqlCommand(strQry, sc);
,你应该没问题。但更好的选择是将其包含在using
子句中,并使用foreach
来浏览dataGridView1.Rows
集合:
foreach(var row in dataGridView1.Rows)
{
using (SqlCommand scmd = new SqlCommand(strQry, sc))
{
scmd.Parameters.AddWithValue("@c0", row.Cells[0].Value);
scmd.Parameters.Add("@po_no", SqlDbType.VarChar).Value = tb_ponumber2.Text;
scmd.Parameters.Add("@invoice", SqlDbType.VarChar).Value = tb_invoice2.Text;
scmd.Parameters.AddWithValue("@c1", row.Cells[1].Value);
scmd.Parameters.Add("@date", SqlDbType.Date).Value = date;
scmd.Parameters.AddWithValue("@c2", row.Cells[2].Value);
scmd.Parameters.AddWithValue("@c3", row.Cells[3].Value);
scmd.Parameters.AddWithValue("@c4", row.Cells[4].Value);
scmd.Parameters.AddWithValue("@c5", row.Cells[5].Value);
scmd.Parameters.AddWithValue("@c6", row.Cells[6].Value);
scmd.Parameters.AddWithValue("@c7", row.Cells[7].Value);
scmd.Parameters.AddWithValue("@c8", row.Cells[8].Value);
scmd.Parameters.AddWithValue("@c9", row.Cells[9].Value);
scmd.ExecuteNonQuery();
MessageBox.Show("Successfully Updated!");
}
}
using
子句的好处是它会在退出子句之前自动调用对象上的Dispose()
方法。