1
生病得到直奔point.I有两个表:不能添加或更新子行,外键约束失败
- 客户信息
- 订单
因为客户可以有0或更多的顺序我使用Index在这两个表之间创建了一个关系。母公司名称中的主键是主键,而公司名称是索引。
- 我知道数据类型和大小和引擎应该是相同的。
- 我也知道公司名称应该存在于父表中以便我们可以修改子表。
我已经翻了一番检查所有的人,但我仍然得到C#中的错误
“不能添加或更新子行,外键约束失败”
for (int i = 0; i < dtgCart.Rows.Count; i++)
{
command.CommandText = "INSERT INTO order_info (CompanyName,Order_ID,ProductName,Model,Address,Quantity,Price) VALUES(@CompanyName,@Order_ID,@ProductName,@Model,@Address,@Quantity,@Price)";
command.Parameters.AddWithValue("@CompanyName",dtgCart.Rows[i].Cells["CompanyName"]);
command.Parameters.AddWithValue("@Order_ID", dtgCart.Rows[i].Cells["Order_ID"]);
command.Parameters.AddWithValue("@ProductName", dtgCart.Rows[i].Cells["ProductName"]);
command.Parameters.AddWithValue("@Model", dtgCart.Rows[i].Cells["Model"]);
command.Parameters.AddWithValue("@Address", dtgCart.Rows[i].Cells["Address"]);
command.Parameters.AddWithValue("@Quantity",dtgCart.Rows[i].Cells["Quantity"]);
command.Parameters.AddWithValue("@Price",dtgCart.Rows[i].Cells["Price"]);
command.ExecuteNonQuery();
}
Con.Close();
Con.Dispose();
这是插入数据到子表的代码
我应该使用SET FOREIGN_KEY_CHECKS = 0;如果我做什么都 预先感谢您的弊端
你在'dtgCart.Rows [I] .Cells [“公司名称”]'的调试器值检查? –
您正试图插入'order_info',但'CustomerName'表中不存在'CompanyName',因此查询失败。 – CodingYoshi
@CodingYoshi公司名称是PK –