简体   繁体   English

ADO.NET数据适配器

[英]ADO.NET DataAdapter

I have the following error when I call the update method on my dataAdapter, after deleting some rows : 删除一些行后,在我的dataAdapter上调用update方法时,出现以下错误:

The DELETE statement conflicted with the REFERENCE constraint "FK_Eleve_Classe". DELETE语句与REFERENCE约束“ FK_Eleve_Classe”冲突。 The conflict occurred in database "hogwarts", table "dbo.Eleve", column 'FK_classID'. 在数据库“ hogwarts”的表“ dbo.Eleve”的列“ FK_classID”中发生了冲突。 The statement has been terminated. 该语句已终止。

Indeed, there is a foreign key defined, but since the primary key is in the "classe" table, why doesn't it allow me to delete my row in "eleve"? 确实,已经定义了一个外键,但是由于主键位于“ classe”表中,为什么它不允许我删除“ eleve”中的行?

Maybe does it try to delete in cascade? 也许它尝试级联删除? (then how to change this?) (然后如何更改?)

edit 1 : SQL 编辑1:SQL

CREATE TABLE [dbo].[Classe](
    [id] [int] NOT NULL,
 CONSTRAINT [PK_Classe] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[Eleve](
    [id] [int] NOT NULL,
    [nom] [varchar](50) NULL,
    [prenom] [varchar](50) NULL,
    [birthdate] [date] NULL,
    [FK_classID] [int] NULL,
 CONSTRAINT [PK_Eleve] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



ALTER TABLE [dbo].[Eleve]  WITH CHECK ADD  CONSTRAINT [FK_Eleve_Classe] FOREIGN KEY([FK_classID])
REFERENCES [dbo].[Classe] ([id])

Edit 2 : some c# code 编辑2:一些C#代码

 DataSet ds = new DataSet();

 c.Open(); // SqlConnection


 SqlDataAdapter da = new SqlDataAdapter("Select * from eleve", (SqlConnection)c);
 da.Fill(ds, "eleves");
 da.SelectCommand = new SqlCommand("select * from cours", (SqlConnection)c);
 da.Fill(ds, "COURS");
 da.SelectCommand = new SqlCommand("select * from professeur", (SqlConnection)c);
 da.Fill(ds, "PROF");
 da.SelectCommand = new SqlCommand("select * from classe", (SqlConnection)c);
 da.Fill(ds, "CLASSE");
 c.Close();


 ds.Tables["eleves"].Rows[0].Delete();

edit 3 : deleting 编辑3:删除

here is the way i delete my element in my table : 这是我删除表中元素的方式:

ds.Tables["eleves"].Rows[0].Delete();

must I set the deleteCommand manually in addition to this? 除此以外,我还必须手动设置deleteCommand吗?


and yes, hogwarts since I'm making a sample to explains it a funny way ;) 是的,霍格沃茨,因为我正在制作一个样本来以有趣的方式解释它;)

Thanks, 谢谢,

KiTe 风筝

Do you have a trigger? 你有扳机吗? This is the usual cause of such errors. 这是此类错误的常见原因。

If eleve is a child table with child rows of classes (eg 0 to many rows in eleve for each row in classe) then this error should not happen. 如果eleve是具有子类行的子表(例如,对于classe中的每一行,eleve中有0到许多行),则不应发生此错误。 Unless the FK is the wrong way around 除非FK走错路

BTW: a cascade is from parent to child. 顺便说一句:级联是从父母到孩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM