INSERT 语句与 FOREIGN KEY 约束冲突
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
txtCategory.ValueMember
的值似乎与数据库中的有效类别 ID 不对应。It appears that the value of
txtCategory.ValueMember
does not correspond to a valid category ID in your database.我认为有两种情况:
1.
在您的表 dbo.tblBook 中,它有一个名为 CatID 的列,作为 dbo.tblCategory 表的外键引用。您似乎正在尝试使用尚未创建的 CatID 属性。如果是这样,请返回并先创建一个 CatID。
2.
也许有多个外键与您的表相关。
如果您使用 sql server,那么下面的 sql 语句将帮助您找到所有这些,检查出来并删除剩余的,祝您好运!
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!