Decreasing a value with a named parameter doesn't change said value

◇◆丶佛笑我妖孽 提交于 2020-01-11 12:32:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!