我的for语句为什么只执行了一次

mpmxs 2010-03-22 02:38:03
我想使用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();
}
...全文
180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
icehaopan 2010-03-22
  • 打赏
  • 举报
回复
代码原来还可以这么写!!!
ghscsdn 2010-03-22
  • 打赏
  • 举报
回复
建议还是好好设计下架构,再去编码,这样做项目以后有改动估计你自己都找不见改什么地方
不懂装懂 2010-03-22
  • 打赏
  • 举报
回复
lz最好重新检查sql语句,或是try catch下
newes_li 2010-03-22
  • 打赏
  • 举报
回复
看上去好复杂,其实很简单
septemson 2010-03-22
  • 打赏
  • 举报
回复
第二段
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);

这两个单引号是错误的,去掉
不懂装懂 2010-03-22
  • 打赏
  • 举报
回复
SqlCommand cmddelempty = new SqlCommand("delete kucun_shangpin where shuliang<0.01", conyewu);
这错了
delete from kucun_shangpin where shuliang<0.01
Python 2010-03-22
  • 打赏
  • 举报
回复
最好跟踪一下,怀疑是发生了异常,看上去好像只执行了一次。
zzxap 2010-03-22
  • 打赏
  • 举报
回复
spcount <= dataGridView2.Rows.Count-1
wonstart 2010-03-22
  • 打赏
  • 举报
回复
dataGridView2.Rows.Count的值查一下。再不行就跟踪一下吧。
不懂装懂 2010-03-22
  • 打赏
  • 举报
回复
....真乱
qlzf11140820 2010-03-22
  • 打赏
  • 举报
回复
spcount < dataGridView2.Rows.Count

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧