使用 C# OleDbConnection 的 Microsoft Access UPDATE 命令和命令不起作用 [英] Microsoft Access UPDATE command using C# OleDbConnection and Command NOT working

查看:38
本文介绍了使用 C# OleDbConnection 的 Microsoft Access UPDATE 命令和命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我正在使用 Microsoft Access,因为力量更大,并且尝试更新记录但没有运气.

I'm using Microsoft Access unfortunately because of higher forces and trying to update a record with no luck.

这是代码:

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();
    }
}

一切似乎都很好,也不例外,但也没有影响行数.它总是返回 0.我在调试时检查了这些值,它应该保持正确.使用 MS Access 2007 创建的访问文件,但其类型为 2002-2003.

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.

知道我做错了什么吗?

推荐答案

试试

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

按照语句的顺序添加参数,即名字...id

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

这篇关于使用 C# OleDbConnection 的 Microsoft Access UPDATE 命令和命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆