这段代码给我这样的错误“更新语句与Reference约束外键冲突” [英] this code give me the error like this "the update statement conflicted with the Reference constraint foreign key "

查看:409
本文介绍了这段代码给我这样的错误“更新语句与Reference约束外键冲突”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(cs);
                con.Open();

                string cb = "update Product set ProductName='" + txtProductName.Text + "',Category='" + cmbCategory.Text + "', Company='" + cmbCompany.Text + "' where Productname='" + textBox1.Text + "'";
                cmd = new SqlCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Autocomplete();
                btnUpdate.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

推荐答案

请阅读我对该问题的评论。您的代码没有告诉我们任何有关问题的信息。错误消息表示您正在尝试传递与存在的值不对应的值,即:类别表(PK)中不存在类别名称(FK)。



看看这里:更新语句与REFERENCE约束冲突 [ ^ ]
Please, read my comment to the question. Your code does not telling us anything about an issue. The error message is saying that you're trying to pass value(s) which does not corresponds to existsing one, i.e.: Category name (FK) does not exists in Category table (PK).

Have a look here: UPDATE statement conflicted with the REFERENCE constraint[^]


这篇关于这段代码给我这样的错误“更新语句与Reference约束外键冲突”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆