使用命名参数减少值不会更改所述值 [英] Decreasing a value with a named parameter doesn't change said value
问题描述
我想减小Access数据库中的urunadedi
值.我有以下代码:
I want to decrease my urunadedi
value in an Access database. I have the following code:
cmd2.Connection = con;
cmd2.Parameters.AddWithValue("@urunid", Convert.ToInt64(textBox1.Text));
cmd2.Parameters.AddWithValue("@hesaplam",Convert.ToInt64(textBox2.Text));
cmd2.CommandText = @"UPDATE Table1
SET urunadedi=urunadedi-@hesaplam
WHERE urunadi=@urunid";
cmd2.ExecuteNonQuery();
但是字段urunadedi
的值没有减少.为什么会这样,我该如何解决?
But the value of the field urunadedi
is not decreasing. Why is that and how can I solve that?
For the benefit of non-Turkish readers, urunadedi, urunid and urunadi translate roughly as ProductName, and hesaplam translates roughly as calculate.
推荐答案
Microsoft OLEDB 忽略参数名称,仅注意参数出现在 order 中CommandText.所以,对于
Microsoft OLEDB ignores parameter names and only pays attention to the order in which the parameters appear in the CommandText. So, for
cmd2.CommandText = @"UPDATE Table1
SET urunadedi=urunadedi-@hesaplam
WHERE urunadi=@urunid";
我们需要首先添加@hesaplam
参数,因为它首先出现在CommandText中
we need to add the @hesaplam
parameter first since it appears first in the CommandText
cmd2.Parameters.AddWithValue("@hesaplam", Convert.ToInt64(textBox2.Text));
cmd2.Parameters.AddWithValue("@urunid", Convert.ToInt64(textBox1.Text));
还请注意,由于忽略了OLEDB参数名称,因此很常见地看到问号(?
)被用作参数占位符:
Note also that because the OLEDB parameter names are ignored it is quite common to see the question mark (?
) being used as the parameter placeholder:
cmd2.CommandText = @"UPDATE Table1
SET urunadedi=urunadedi-?
WHERE urunadi=?";
cmd2.Parameters.AddWithValue("?", Convert.ToInt64(textBox2.Text)); // @hesaplam
cmd2.Parameters.AddWithValue("?", Convert.ToInt64(textBox1.Text)); // @urunid
这篇关于使用命名参数减少值不会更改所述值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!