Is order of parameters for database Command object really important?

后端 未结 3 974
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 02:46

I was debugging a database operation code and I found that proper UPDATE was never happening though the code never failed as such. This is the code:

               


        
3条回答
  •  失恋的感觉
    2020-12-04 03:18

    The order is important because of the use of ? placeholders in the command string.

    If you want to list the parameters in any order, it's best to use named parameters, such as @word, @sentence, etc.

    condb.Open();
    OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=@word,sentence=@sentence,mp3=@mp3 WHERE id=@id AND exercise_id=@exercise_id", condb);
    dbcom.Parameters.AddWithValue("@id", wd.ID);
    dbcom.Parameters.AddWithValue("@exercise_id", wd.ExID);
    dbcom.Parameters.AddWithValue("@word", wd.Name);
    dbcom.Parameters.AddWithValue("@sentence", wd.Sentence);
    dbcom.Parameters.AddWithValue("@mp3", wd.Mp3);                         
    

提交回复
热议问题