我收到这个错误
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK__Subjects__fk_sub__6383C8BA".
The conflict occurred in database "The Library", table "dbo.Subjects", column '_id'.
这_id
是身份、独特和主要的。并且应该在内容输入数据库时自动生成。
我的用于创建表的 sql。
create table Subjects
(
_id integer identity not null,
unique(_id),
primary key(_id),
subject varchar(50) not null,
[content] varchar(MAX) not null,
fk_subfolderTo integer not null,
foreign key(fk_subfolderTo) references Subjects(_id),
signed varchar(150) not null,
fk_writer integer not null,
foreign key(fk_writer) references Users(_id)
)
以及用于插入表格的 C# 代码。
public static void Create (SqlConnection con, int userId, string subject, string content, string signed, int subfolderTo)
{
using (var command = new SqlCommand("Insert into Subjects (subject, [content], fk_subfolderTo, signed, fk_writer) values ('" + subject + "', '" + content + "', " + subfolderTo + ", '" + signed + "', " + userId + ")", con))
{
command.ExecuteNonQuery();
}
}
我正在使用 MSSQL。