简体   繁体   English

需要帮助减去数据库列中的值

[英]need help minus value from database column

I am trying to minus value in database column from textbox I got error an expression of non-boolean type specified in a context where condition is expected 我正在尝试从文本框中减去数据库列中的值,但出现错误,在预期条件的上下文中指定了非布尔类型的表达式

  cn.Open();
  SqlCommand command = new SqlCommand();
  command.Connection = cn;

  command.CommandText = "select * from class where  quanitity - '"+Convert.ToInt32(textBox10.Text)+"'";
  command.ExecuteNonQuery();

  cn.Close();

您不是要减去column的值。您想要这样

        command.CommandText = "update class set quanitity = quanitity - "+Convert.ToInt32(textBox10.Text) ;

这肯定会工作

command.CommandText = string.Format("update class set quanitity= quanitity - {0}",Convert.ToInt32(textBox10.Text));

You need a value to compare your two numbers to. 您需要一个值来比较两个数字。 You are subtracting your TextBox10 value from quantity, but what result set do you want to see after subtracting the value? 您正在从数量中减去TextBox10的值,但是在减去该值之后您想看到什么结果集? There needs to be a comparison somewhere to the right of your WHERE. 您的位置右侧需要进行比较。

command.CommandText = "select (quantity - "+Convert.ToInt32(textBox10.Text)+") from class ;

I suspect you SQL Injection alert ..Do not Concatenate string it's wide open for sql injection alert.So use parameterized query 我怀疑您是SQL注入警报 ..不要连接字符串,它对于sql注入警报是开放的。所以请使用参数化查询

command.CommandText = "select * from class where  quanitity -@txtbox10";
command.Parameters.AddWithValue("@txtbox10", Convert.ToInt32(textBox10.Text));
command.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM