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/