Microsoft Access UPDATE Team Using C # OleDbConnection and Command NOT working

I use Microsoft Access, unfortunately, due to higher forces and attempts to update the record without any luck.

This is the code:

private void UpdateContact(Contact contact)
{
    using (OleDbConnection db = new OleDbConnection(_connString))
    {
        string query = "UPDATE [Contact] SET [FirstName] = @FirstName, [LastName] = @LastName, [MobileNumber] = @MobileNumber WHERE [Id] = @Id";

        OleDbCommand cmd = new OleDbCommand(query, db) { CommandType = CommandType.Text };
        cmd.Parameters.AddWithValue("@Id", contact.Id);
        cmd.Parameters.AddWithValue("@FirstName", contact.FirstName);
        cmd.Parameters.AddWithValue("@LastName", contact.LastName);
        cmd.Parameters.AddWithValue("@MobileNumber", contact.MobileNumber);

        db.Open();

        int rowsAffected = cmd.ExecuteNonQuery();

        db.Close();
    }
}

Everything seems beautiful, no exceptions to the lines. It always returns 0. I checked the values ​​during debugging and its correctness, which should be preserved. Access file created using MS Access 2007, but its type is in 2002-2003.

Any idea what I'm doing wrong?

+3
source share
2 answers

try

string query = "UPDATE [Contact] SET [FirstName] = ? [LastName] = ?, [MobileNumber] = ? WHERE [Id] = ?"

Add your parameters to the operator order, i.e. firstname ... id

+5
source

Source: https://habr.com/ru/post/1783313/


All Articles