[英]C# , SQL FOREIGN KEY
I am trying to insert data into the table 'table' , but I am stocked with the error of foreign key , 'building_id' from the building table , how do I get through it? 我正在尝试将数据插入表“ table”中,但是我在建筑表中存有外键“ building_id”的错误,我该如何处理呢?
Error prompt: 错误提示:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_user_user".
INSERT语句与FOREIGN KEY约束“ FK_user_user”冲突。
The conflict occurred in database "C:\\USERS\\USER\\DOWNLOADS\\VIUSAL\\VIUSAL\\ENERGYVIUSAL\\ENERGYVIUSAL\\DATABASE1.MDF", table "dbo.building", column 'building_id'.在数据库“ C:\\ USERS \\ USER \\ DOWNLOADS \\ VIUSAL \\ VIUSAL \\ ENERGYVIUSAL \\ ENERGYVIUSAL \\ DATABASE1.MDF”的表“ dbo.building”的“ building_id”列中发生了冲突。 The statement has been terminated.
该语句已终止。
This is the code: 这是代码:
DataRow drowEmployee = dsetEnergyViusal.Tables["Table"].NewRow();
drowEmployee["Name"] = txtName.Text;
drowEmployee["ContactID"] = Convert.ToInt32(txtContactId.Text);
drowEmployee["Company"] = txtCompanyId.Text;
drowEmployee["Building_id"] = txtBuildingID.Text;
drowEmployee["Telephone"] = txtContactNumber.Text;
drowEmployee["Email"] = txtEmail.Text;
drowEmployee["Password"] = txtPassword.Text;
if (radClient.Checked)
drowEmployee["Client"] = radClient.Checked;
else if (radNews.Checked)
drowEmployee["News"] = radNews.Checked;
drowEmployee["Telephone"] = txtContactNumber.Text;
dsetEnergyViusal.Tables["Table"].Rows.Add(drowEmployee);
sqldaEnergyViusal.Update(dsetEnergyViusal, "Table");
MessageBox.Show("Record has been successfully added.", "EnergyViusal", MessageBoxButtons.OK, MessageBoxIcon.Information);
User_register_Load(null, null);
您可能在文本框中输入并在插入查询中发送的建筑物ID在建筑物表中不存在。
May be you will be added Building_Id as a foreign key in the currently inserting table. 可能您将在当前插入表中将Building_Id添加为外键。 to avoid that you have to first insert into Building table and that inserted id has to be used in this insertion ie you have to use a valid building_id in the insertion.
为避免您必须首先插入Building表中,并且必须在此插入操作中使用插入的ID,即必须在插入操作中使用有效的building_id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.