Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

ado.net - Microsoft Access UPDATE command using C# OleDbConnection and Command NOT working

I'm using Microsoft Access unfortunately because of higher forces and trying to update a record with no 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 to be fine, no exception but no rowsAffected either. It always returns 0. I have checked the values while debugging and its the correct that should persist. The access file created with MS Access 2007 but its type is of 2002-2003.

Any idea what am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try

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

Add your parameters in the order of the statement, i.e. firstname...id


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...