Issue in updating MS Access records using oledbcommand.executeNonQuery(), result not updating

后端 未结 1 1476
傲寒
傲寒 2021-01-26 18:21

I am posting a query first time here, So, Please ignore my formatting.

I am trying to update my .accdb file using update command, but result of oledbcommand.execut

相关标签:
1条回答
  • 2021-01-26 18:55

    OleDbCommand doesn't support named parameters. The only matter is their orders.

    From OleDbCommand.Parameters property

    The OLE DB .NET Provider does not support named parameters for passing parameters...

    Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

    That's why your first @Action in OleDbCommand matches with @SNo in your AddWithValue and @SNo matches with your @Action in your AddWithValue.

    Since probably you don't have a data like this, there will be no update operation.

    Switch your parameter orders and use .Add method which is recommended instead of AddWithValue. It may generate unexpected results. Read;

    • Can we stop using AddWithValue() already?

    Also use using statement to dispose your OleDbConnection and OleDbCommand instead of calling .Dispose() and .Close() methods manually.

    using(OleDbConnection vcon = new OleDbConnection(conString))
    using(OleDbCommand vcom = vcon.CreateCommand())
    {
        vcom.CommandText = "UPDATE DefTask_List SET [Action]=@Action WHERE [SNo]=@SNo";
        vcom.Parameters.Add("?", OleDbType.VarChar).Value = comboBox1.Text;
        vcom.Parameters.Add("?", OleDbType.Integer).Value = (int)row.Cells[0].Value;
        // I assume your column types are NVarchar2 and Int32
        vcon.Open();
        int k = vcom.ExecuteNonQuery();
    }
    
    0 讨论(0)
提交回复
热议问题