c# - sql中不能减

标签 c# mysql asp.net

 string sql2 = "UPDATE project_table 
                SET resources=(resources-" + txtresource.Text + ") 
                where code=" + project_code + "";

我使用下面的查询来更新我的表,但我的数据库似乎没有减少资源。我做错了什么?

最佳答案

转换它,然后在查询中使用,例如

int val = Convert.ToInt32(txtresource.Text.Trim());
SET resources = resources - val

最后考虑使用SQLParameter来避免SQL注入(inject)攻击

string sql2 = "UPDATE project_table SET resources = resources - @val where code = @code";
//Create Command
SqlCommand cmd = new SqlCommand(sql2, connectionObject);
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add(SQLDbType.INT, @val).Value = val;
cmd.Parameters.Add(SQLDbType.VARCHAR, @code).Value = project_code;

关于c# - sql中不能减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38085228/

相关文章:

c# - 以编程方式检查 Windows 10 的区分大小写的目录属性

c# - 测试类型是否为集合的最佳方法

python从mysql获取用户并在linux上创建它们

mysql自动计算3列相乘结果插入第四列

javascript - asp.net 中的 HTML 标记注入(inject)

c# - 显示数据库中的数据

c# - 通过 ProxyServer 发送 WCF 请求

mysql - 如何找到不同配对行之间的天数?

jquery - 如何在 anchor 标签上执行点击事件

c# - 是否使用 TDD 进行构造函数注入(inject)?