我想使用for语句把datagridview中的数据更新后再保存到数据库中,为什么每次执行时只添加了第一条记录?
代码如下:
for (int spcount = 0; spcount < dataGridView2.Rows.Count;spcount++ )
{
//添加记录到零售商品表
SqlCommand cmdlinshoushangpin = new SqlCommand("insert into linshou_shangpin values('" + this.dataGridView2.Rows[spcount].Cells[0].Value + "','" + this.dataGridView2.Rows[spcount].Cells[1].Value + "','" +
this.dataGridView2.Rows[spcount].Cells[2].Value + "','" + this.dataGridView2.Rows[spcount].Cells[3].Value + "','" + this.dataGridView2.Rows[spcount].Cells[4].Value + "','" + this.dataGridView2.Rows[spcount].Cells[5].Value + "','" +
this.dataGridView2.Rows[spcount].Cells[6].Value + "','" + Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[7].Value) + "','" + Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[8].Value) + "','" + Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[9].Value) + "','" +
Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[10].Value) + "','" + this.dataGridView2.Rows[spcount].Cells[11].Value + "','" + this.dataGridView2.Rows[spcount].Cells[12].Value + "','" + this.dataGridView2.Rows[spcount].Cells[13].Value + "','" +
this.dataGridView2.Rows[spcount].Cells[14].Value + "','" + this.dataGridView2.Rows[spcount].Cells[15].Value + "')", conyewu);
cmdlinshoushangpin.ExecuteNonQuery(); //更新库存表数据
SqlCommand cmdupdatekucun = new SqlCommand("update kucun_shangpin set shuliang=shuliang-'" + this.dataGridView2.Rows[spcount].Cells[8].Value + "' where sp_id='" + this.dataGridView2.Rows[spcount].Cells[1].Value + "'", conyewu);
cmdupdatekucun.ExecuteNonQuery();
SqlCommand cmddelempty = new SqlCommand("delete kucun_shangpin where shuliang<0.01", conyewu);
cmddelempty.ExecuteNonQuery();
}
代码如下:
for (int spcount = 0; spcount < dataGridView2.Rows.Count;spcount++ )
{
//添加记录到零售商品表
SqlCommand cmdlinshoushangpin = new SqlCommand("insert into linshou_shangpin values('" + this.dataGridView2.Rows[spcount].Cells[0].Value + "','" + this.dataGridView2.Rows[spcount].Cells[1].Value + "','" +
this.dataGridView2.Rows[spcount].Cells[2].Value + "','" + this.dataGridView2.Rows[spcount].Cells[3].Value + "','" + this.dataGridView2.Rows[spcount].Cells[4].Value + "','" + this.dataGridView2.Rows[spcount].Cells[5].Value + "','" +
this.dataGridView2.Rows[spcount].Cells[6].Value + "','" + Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[7].Value) + "','" + Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[8].Value) + "','" + Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[9].Value) + "','" +
Convert.ToInt32(this.dataGridView2.Rows[spcount].Cells[10].Value) + "','" + this.dataGridView2.Rows[spcount].Cells[11].Value + "','" + this.dataGridView2.Rows[spcount].Cells[12].Value + "','" + this.dataGridView2.Rows[spcount].Cells[13].Value + "','" +
this.dataGridView2.Rows[spcount].Cells[14].Value + "','" + this.dataGridView2.Rows[spcount].Cells[15].Value + "')", conyewu);
cmdlinshoushangpin.ExecuteNonQuery(); //更新库存表数据
SqlCommand cmdupdatekucun = new SqlCommand("update kucun_shangpin set shuliang=shuliang-'" + this.dataGridView2.Rows[spcount].Cells[8].Value + "' where sp_id='" + this.dataGridView2.Rows[spcount].Cells[1].Value + "'", conyewu);
cmdupdatekucun.ExecuteNonQuery();
SqlCommand cmddelempty = new SqlCommand("delete kucun_shangpin where shuliang<0.01", conyewu);
cmddelempty.ExecuteNonQuery();
}
这错了
delete from kucun_shangpin where shuliang<0.01
SqlCommand cmdupdatekucun = new SqlCommand("update kucun_shangpin set shuliang=shuliang-'" + this.dataGridView2.Rows[spcount].Cells[8].Value + "' where sp_id='" + this.dataGridView2.Rows[spcount].Cells[1].Value + "'", conyewu);这两个单引号是错误的,去掉