none
Parameterized update query RRS feed

  • Question

  • I have this piece of code:

                OleDbCommand ocdUpdateETypeofD = new OleDbCommand("update wordsdetails set etid = @etid where id = @id",ocnEnglishVocabularyNotebook);
                ocdUpdateETypeofD.Parameters.Add("@id", OleDbType.Integer);
                ocdUpdateETypeofD.Parameters.Add("@etid", OleDbType.Integer);
                ocdUpdateETypeofD.Parameters["@id"].Value = 31;
                ocdUpdateETypeofD.Parameters["@etid"].Value = 2;
                try { ocdUpdateETypeofD.ExecuteNonQuery(); }...

    it doesn't throw any exception but it doesn't change the record in my ms access 2013 database when I check it.

    I tried changing the commandtext like below:

    "update wordsdetails set etid = 3 where id = 31"

    then it worked well. I also set a breakpoint and debugged the app and realized that the values where just like above and it didn't work.

    can you see what the problem is?

    thanks in advance

    • Moved by Youjun Tang Wednesday, April 1, 2015 5:47 AM more related to the suggested forum
    Tuesday, March 31, 2015 4:02 PM

Answers

  • Your problem is simple. Despite you can use in OleDB alphabetic parameters, are it not named parameters. It are just placeholder. You can use every character (most use the questionmark).

    Parameters are handled in the order which they appear in the SQL expression. By you that is wrong. 

    (Also use the AddWithValue instead of your outdated one. 

    ocdUpdateETypeofD.Parameters.AddWithValue("@id",31);


    Success
    Cor



    Thursday, April 2, 2015 3:46 PM

All replies

  • Hi anoushiravan,

    Based on your description, your issue is more related with the ado.net, and this forum is discussing about Data Controls and Databinding like datagridview. I will move this thread to the ado.net forum for help.

    Reference:

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetdataset

    Thanks for your understanding.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 1, 2015 5:45 AM
  • Hi anoushiravan,

    You code seems good, I'm afraid that the ocnEnglishVocabularyNotebook has something wrong.

    You could set the Provider to "Microsoft.ACE.OLEDB.12.0" for accdb file.

    string mdbPath = System.IO.Path.Combine(Application.StartupPath, "db1.accdb");
    OleDbConnection ocnEnglishVocabularyNotebook = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + mdbPath); 

    if your access file is mdb file. set the Provider to "Microsoft.JET.OLEDB.4.0".

    string mdbPath = System.IO.Path.Combine(Application.StartupPath, "db1.mdb");
    OleDbConnection ocnEnglishVocabularyNotebook = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + mdbPath);

    If you have any other concern regarding this issue, please feel free to let me know.

    Best regards,
    Youjun Tang



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 1, 2015 9:31 AM
  • Your problem is simple. Despite you can use in OleDB alphabetic parameters, are it not named parameters. It are just placeholder. You can use every character (most use the questionmark).

    Parameters are handled in the order which they appear in the SQL expression. By you that is wrong. 

    (Also use the AddWithValue instead of your outdated one. 

    ocdUpdateETypeofD.Parameters.AddWithValue("@id",31);


    Success
    Cor



    Thursday, April 2, 2015 3:46 PM