C# 如何在C中使用sql查询向现有值添加值#
如何更新字段以向现有值添加值? 例如,我有下面的产品表C# 如何在C中使用sql查询向现有值添加值#,c#,sql,asp.net,C#,Sql,Asp.net,如何更新字段以向现有值添加值? 例如,我有下面的产品表 product quantity x 4 y 5 z 3 有没有办法简单地增加数量的价值? 像 我想将文本框值添加到4,以便在product='x'处更新信用 我收到错误“无效列名'qn'”(group1=group1+qn)。请建议我解决这个错误 删除SQL字符串中参数周围的单引
product quantity
x 4
y 5
z 3
有没有办法简单地增加数量的价值?
像
我想将文本框值添加到4,以便在product='x'处更新信用
我收到错误“无效列名'qn'”(group1=group1+qn)。请建议我解决这个错误 删除SQL字符串中参数周围的单引号
"update product set group1 = group1 + qn where productname = '@productname'"
//^^ ^^
使用参数时不需要单引号
还为qn
添加参数。
比如:
最好使用语句将命令和连接对象包含在
中,这样可以确保正确地处理资源 您必须添加qn
作为另一个参数,并随查询一起传递,因此您的查询如下所示:
SqlCommand cmd1 = new SqlCommand("update product set group1 = group1 + @qn where productname = @productname", con);
cmd1.Parameters.Add(new SqlParameter("@qn", qn));
cmd1.Parameters.Add(new SqlParameter("@productname", TextBox1.Text));
您的表没有group1列我在更新中提到数量为group1query@Meena为什么?为什么不使用真实的列名呢?group1不是列名。它的数量。我的代码运行良好,以后我将添加'using'语句。谢谢你的建议,。。。
int qn = int.Parse(TextBox3.Text)
SqlCommand cmd1 = new SqlCommand("update product set group1 = group1 + @qn where productname = @productname", con);
cmd1.Parameters.Add(new SqlParameter("@productname", TextBox1.Text));
cmd1.Parameters.Add(new SqlParameter("@qn", qn));
SqlCommand cmd1 = new SqlCommand("update product set group1 = group1 + @qn where productname = @productname", con);
cmd1.Parameters.Add(new SqlParameter("@qn", qn));
cmd1.Parameters.Add(new SqlParameter("@productname", TextBox1.Text));