如何将listview中的项目与SQL Server上的其他项目相加 [英] How to sum items in listview to others on SQL server
本文介绍了如何将listview中的项目与SQL Server上的其他项目相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将listview中的每个项目加到表中,但我总是得到空单元格
我尝试过的方法:
string sql = UPDATE itmscat1 SET [itmcount] = [itmcount] + @itmcount WHERE [itmcode] = @itmcode;
cm.CommandText = sql;
foreach (ListViewItem item in listView1.Items)
{
cm.Parameters.Clear();
cm.Parameters.Clear();
SqlParameter par_ItmCount = new SqlParameter( @ itmcount,SqlDbType.Int);
par_ItmCount.Value = item.SubItems [ 4 ]。文字;
cm.Parameters.Add(par_ItmCount);
SqlParameter par_ItmCode = new SqlParameter( @ itmcode,SqlDbType.Int);
par_ItmCode.Value = item.SubItems [ 1 ]。文字;
cm.Parameters.Add(par_ItmCode);
{
cm.ExecuteNonQuery();
}
}
解决方案
您的参数设置为int但您传递的是字符串值。确保对象类型相同(int)。
将列表项转换为int。
iepar_ItmCode.Value = Convert.ToInt16(Item.SubItems [ 4 ]。Text);
I try to sum each item in listview to table but I always get empty cells
What I have tried:
string sql = "UPDATE itmscat1 SET [itmcount] = [itmcount] + @itmcount WHERE [itmcode] = @itmcode";
cm.CommandText = sql;
foreach (ListViewItem item in listView1.Items)
{
cm.Parameters.Clear();
cm.Parameters.Clear();
SqlParameter par_ItmCount = new SqlParameter("@itmcount", SqlDbType.Int);
par_ItmCount.Value = item.SubItems[4].Text;
cm.Parameters.Add(par_ItmCount);
SqlParameter par_ItmCode = new SqlParameter("@itmcode", SqlDbType.Int);
par_ItmCode.Value = item.SubItems[1].Text;
cm.Parameters.Add(par_ItmCode);
{
cm.ExecuteNonQuery();
}
}
解决方案
Your parameters are set as int yet you are passing string values. Make sure the object type are the same (int).
convert the list items to int.
iepar_ItmCode.Value = Convert.ToInt16(Item.SubItems[4].Text);
这篇关于如何将listview中的项目与SQL Server上的其他项目相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文