【问题标题】:C# Access Db update query not Updating when data is changedC# Access Db更新查询在数据更改时不更新
【发布时间】:2022-09-27 10:46:26
【问题描述】:

这是我编写的代码,但即使在成功的 messageBox 之后它也不会更改数据库中的任何数据

 connection.Open();
            command = new OleDbCommand(\"UPDATE employeeTable SET fullname=@fullname, [gender]=@gender, [dept]=@dept, [sector]=@sector, [sub_sector]=@sub_sector, [timetable]=@timetable WHERE empid=@empid\", connection);



            command.Parameters.AddWithValue(\"@empid\", txtEmpID.Text);
            command.Parameters.AddWithValue(\"@fullname\", txtName.Text);
            command.Parameters.AddWithValue(\"@gender\", cboGender.SelectedItem.ToString());
            command.Parameters.AddWithValue(\"@dept\", cboCompany.SelectedItem.ToString());
            command.Parameters.AddWithValue(\"@sector\", cboSector.SelectedItem.ToString());
            command.Parameters.AddWithValue(\"@sub_sector\", cboSub.SelectedItem.ToString());
            command.Parameters.AddWithValue(\"@timetable\", cboTimetable.SelectedItem.ToString());
            command.ExecuteNonQuery();
            connection.Close();
            MessageBox.Show(\"Record Updated Successfully!\", \"NEW EMPLOYEE ADDED\", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
  • @empid 在数据表中吗?更新没有向表中添加新记录
  • empid 是每个用户在创建新用户期间拥有的工作 ID。所以每个员工都有不同的ID。所以我想用它来选择表就是为什么。但无论更新是否成功保存,它都不会在数据库中更新

标签: c# database ms-access sql-update


【解决方案1】:

我没有运行你的代码,所以你可能想尝试使用

 command.ExecuteScalar();

代替

command.ExecuteNonQuery();

还要尽量避免直接对数据库表使用 SQL 保留字字段像 LEVEL 等,这样你就可以避免 SQL 错误。

【讨论】:

  • 好的,非常感谢,我会努力的
  • 不客气。大约在 2006 年,我发现我在查询中使用的一些关键字使 sql 查询在 Visual Basic 6.0 中失败。从那时起,像 Session、Level 等词是 SQL Admin 管理关键字,在查询中使用时会返回错误或可能无法按预期工作,因此在我的项目中没有完全按原样使用。我的建议是,如果您正在设计学校管理应用程序,您可能会使用 stdsession 用于学生会话,stdlevel 用于学生级别等...
  • 我正在开发一种员工软件来记录工具和收集的工具
  • 只需将您的字段设置为员工姓名的 empname 等等......
【解决方案2】:

您的 OleDbCommand 和 Parameters 中的参数排列必须相同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-20
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多