简体   繁体   English

C# Access Db update query not Updating when data is changed

[英]C# Access Db update query not Updating when data is changed

This is the code i wrote but it's not changing any data in the database even after a successful messageBox这是我编写的代码,但即使在成功的 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);

The arrangement of parameters in your OleDbCommand and Parameters must be the same您的 OleDbCommand 和 Parameters 中的参数排列必须相同

I didn't get to run your code so you might want to try using我没有运行你的代码,所以你可能想尝试使用

 command.ExecuteScalar();

instead of代替

command.ExecuteNonQuery();

Also try to avoid direct use of SQL Reserve words for your database table Fields like LEVEL etc so that you avoid SQL Errors.还要尽量避免直接使用 SQL 为您的数据库表保留字,如 LEVEL 等字段,以避免 SQL 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM