问题
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();
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.
回答1:
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";
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));
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
来源:https://stackoverflow.com/questions/23981768/decreasing-a-value-with-a-named-parameter-doesnt-change-said-value