INSERT 语句与 FOREIGN KEY 约束冲突

发布于 2024-11-15 17:26:39 字数 836 浏览 6 评论 0原文

myCommand = New SqlCommand(
"INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, 
                     Price, EnterDate, CatID, RackID, Amount) 
VALUES('" & txtBookCode.Text & "','" & 
            txtTitle.Text & "','" & 
            txtAuthor.Text & "','" & 
            txtPublishYear.Text & "','" & 
            txtPrice.Text & "', #" & 
            txtEnterDate.Text & "#, " & 
            txtCategory.ValueMember & "," & 
            txtRack.ValueMember & "," & 
            txtAmount.Text & ")"
, myConnection)

错误是:

INSERT 语句与 FOREING KEY 约束“FK_tblBook_tblCategory”冲突。这 数据库“CIEDC”、表“dbo.tblCategory”、列“CatID”中发生冲突。 该声明已终止。

这是因为我的数据库关系吗?

如何解决这个问题?

myCommand = New SqlCommand(
"INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, 
                     Price, EnterDate, CatID, RackID, Amount) 
VALUES('" & txtBookCode.Text & "','" & 
            txtTitle.Text & "','" & 
            txtAuthor.Text & "','" & 
            txtPublishYear.Text & "','" & 
            txtPrice.Text & "', #" & 
            txtEnterDate.Text & "#, " & 
            txtCategory.ValueMember & "," & 
            txtRack.ValueMember & "," & 
            txtAmount.Text & ")"
, myConnection)

The error was:

The INSERT statement conflicted with the FOREING KEY constraint "FK_tblBook_tblCategory". The
conflict occurred in database "CIEDC", table "dbo.tblCategory", column 'CatID'.
The statement has been terminated.

Was that because of my database's relationships?

How can this be solved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-11-22 17:26:39

txtCategory.ValueMember 的值似乎与数据库中的有效类别 ID 不对应。

It appears that the value of txtCategory.ValueMember does not correspond to a valid category ID in your database.

沫尐诺 2024-11-22 17:26:39

我认为有两种情况:

1.

在您的表 dbo.tblBook 中,它有一个名为 CatID 的列,作为 dbo.tblCategory 表的外键引用。您似乎正在尝试使用尚未创建的 CatID 属性。如果是这样,请返回并先创建一个 CatID

2.

也许有多个外键与您的表相关。
如果您使用 sql server,那么下面的 sql 语句将帮助您找到所有这些,检查出来并删除剩余的,祝您好运!

    USE <database_name>;  
GO  
SELECT   
    f.name AS foreign_key_name  
   ,OBJECT_NAME(f.parent_object_id) AS table_name  
   ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name  
   ,OBJECT_NAME (f.referenced_object_id) AS referenced_object  
   ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name  
   ,is_disabled  
   ,delete_referential_action_desc  
   ,update_referential_action_desc  
FROM sys.foreign_keys AS f  
INNER JOIN sys.foreign_key_columns AS fc   
   ON f.object_id = fc.constraint_object_id   
--WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee'); 
GO 
SELECT 
f.name AS foreign_key_name 
,OBJECT_NAME(f.parent_object_id) AS table_name 
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name 
,OBJECT_NAME (f.referenced_object_id) AS referenced_object 
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name 
,is_disabled 
,delete_referential_action_desc 
,update_referential_action_desc 
FROM sys.foreign_keys AS f 
INNER JOIN sys.foreign_key_columns AS fc 
ON f.object_id = fc.constraint_object_id 
--WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee');

I think there are two cases:

1.

In your table dbo.tblBook, it has a column named CatID act as foreign key reference to dbo.tblCategory table. It seems that you're trying to use CatID property that haven't been created yet.if so, go back and create a CatID first.

2.

Maybe there are more than one foreign key that related to your table.
if you're using sql server so below sql statement will help you find all of them, check it out and drop the leftover, good luck!

    USE <database_name>;  
GO  
SELECT   
    f.name AS foreign_key_name  
   ,OBJECT_NAME(f.parent_object_id) AS table_name  
   ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name  
   ,OBJECT_NAME (f.referenced_object_id) AS referenced_object  
   ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name  
   ,is_disabled  
   ,delete_referential_action_desc  
   ,update_referential_action_desc  
FROM sys.foreign_keys AS f  
INNER JOIN sys.foreign_key_columns AS fc   
   ON f.object_id = fc.constraint_object_id   
--WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee'); 
GO 
SELECT 
f.name AS foreign_key_name 
,OBJECT_NAME(f.parent_object_id) AS table_name 
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name 
,OBJECT_NAME (f.referenced_object_id) AS referenced_object 
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name 
,is_disabled 
,delete_referential_action_desc 
,update_referential_action_desc 
FROM sys.foreign_keys AS f 
INNER JOIN sys.foreign_key_columns AS fc 
ON f.object_id = fc.constraint_object_id 
--WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文