繁体   English   中英

C# - 无法添加或更新子行:外键约束失败

[英]C# - Cannot add or update a child row: a foreign key constraint fails

我知道有很多关于这个问题的问题,但我无法解决它。 我正在做一个图形界面来添加/删除员工和管理缺勤。

我一直在阅读和测试这些建议stackOverflow1但我无法更改数据库,因为它已经由我们的教授给出。

外键的值已经在我在代码上称为“部门”的表“服务”中。

我有以下数据库(对于我遇到问题的表):

DROP TABLE IF EXISTS personnel;
CREATE TABLE personnel (
  IDPERSONNEL int NOT NULL,
  IDSERVICE int NOT NULL,
  NOM varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRENOM varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  TEL varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  MAIL varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS service;
CREATE TABLE service (
  IDSERVICE int NOT NULL,
  NOM varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

在应用程序中,您可以在此处和下图看到,我必须修改员工数据并将其再次保存到 dataGridView。

单击保存后,我遇到了问题。 我的请求有错误,但我不知道如何解决。
无法添加或更新子行:外键约束失败( gestionpersonnel personnel CONSTRAINT personnel_ibfk_1 FOREIGN KEY ( IDSERVICE ) REFERENCES service ( IDSERVICE ))

这是我在 class AccessDataBase 中使用的方法:

  public static void UpdateEmployee(Employee employee)
        {
            string req = "update personnel set nom = @nom, prenom = @prenom, tel = @tel, mail = @mail, idservice = @idservice ";
            req += "where idpersonnel = @idpersonnel;";
            Dictionary<string, object> parameters = new Dictionary<string, object>
            {
                {"@idpersonnel", employee.IdEmployee },
                {"@idservice", employee.IdDepartment },
                {"@nom", employee.FamilyName },
                {"@prenom", employee.FirstName },
                {"@tel", employee.Phone },
                {"@mail", employee.Mail }
            };
            ConnexionDataBase connexion = ConnexionDataBase.GetInstance(connexionString);
            connexion.ReqUpdate(req, parameters);
        }

更新1:

在此处输入图像描述 对于字符串变量中的请求,我一直在数据库和 C# 中的代码之间遵循相同的顺序。

应用程序的屏幕截图

更新2(回复@Sharath NS),通过使用一步一步,我可以从部门得到一个值在此处输入图像描述

更新 3(回复@Crowcoder):确实是@Sharath NS 给我的相同评论。 我有一种感觉,我得到了部门的领域,而不是 idDepartment。

这是我保存按钮的代码:

 private void BtnSaveEmployee_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxFamilyName.Text) ||
                String.IsNullOrEmpty(textBoxFirstName.Text) ||
                String.IsNullOrEmpty(textBoxPhone.Text) ||
                String.IsNullOrEmpty(textBoxMail.Text))
            {
                MessageBox.Show("All the informations shall be filled", "Information");
            }
            else
            
            {
                Department department = (Department)bindingSourceDepartments.List[bindingSourceDepartments.Position];
                int idEmployee = 0;
                if (modificationOngoing)
                {
                    idEmployee = (int)dataGridViewEmployee.SelectedRows[0].Cells["idEmployee"].Value;
                }
                Employee employee = new Employee(idEmployee,
                                                 textBoxFamilyName.Text,
                                                 textBoxFirstName.Text,
                                                 textBoxPhone.Text,
                                                 textBoxMail.Text,
                                                 department.IdDepartment,
                                                 department.DepartmentName);

                if (modificationOngoing)
                {
                    controlMyApp.UpdateEmployee(employee);
                    modificationOngoing = false;
                    grpBoxEmployee.Enabled = true;
                    lblShowButtonClicked.Text = "Adding";
                }
                else
                {
                    controlMyApp.AddEmployee(employee);
                }
                FillEmployeesList();
                EmptyEmployeeSelection();
            }
        }

在此处输入图像描述

请检查更新员工详细信息时发送的 IDservice 的值。 从错误看来,父表服务中不存在正在更新的 IdService 值。

暂无
暂无

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

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