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

前端 未结 1 1293
感动是毒
感动是毒 2021-01-24 08:24

I want to decrease my urunadedi value in an Access database. I have the following code:

cmd2.Connection = con;
cmd2.Parameters.AddWithValue(\"@uruni         


        
相关标签:
1条回答
  • 2021-01-24 08:57

    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
    
    0 讨论(0)
提交回复
热议问题