“无法启用此约束,因为并非所有值都有对应的父值."

编程入门 行业动态 更新时间:2024-10-26 00:29:00
本文介绍了“无法启用此约束,因为并非所有值都有对应的父值."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个嵌套的XML DataTable,但是由于某种原因,它总是使我抛出错误:由于并非所有值都有对应的父值,因此无法启用此约束. 这是我希望得到的XML结果:

I want to create a nested XML DataTable but for some reason, it keeps throwing me the error: This constraint cannot be enabled as not all values have corresponding parent values. This is the XML result that I am hoping for:

<Annotation> <Information> <folder>Kangaroo</folder> <filename>00001.JPG</filename> </Information> <size> <width>1024</width> <height>1024</height> <depth>3</depth> </size> <object> <bndbox> <ID>1</ID> <xmin>143</xmin> <ymin>163</ymin> <xmax>332</xmax> <ymax>335</ymax> </bndbox> </object> </Annotation>

我尝试将 bndbox 中的id设置为主键,创建外键约束,并在 Relations.Add <中设置 createConstraints 参数./code>方法为false.这三个中的后者创建了一个XML文件,但是它根本没有创建如下所示的 object 容器:

I have tried setting the id in bndbox as a primary key, creating a foreign key constraint, and setting the createConstraints parameter in the Relations.Add method to false. The latter of the three created an XML file but it did not create the object container at all which looks like this:

<Annotation> <Information> <folder>Kangaroo</folder> <filename>00001.JPG</filename> </Information> <size> <width>1024</width> <height>1024</height> <depth>3</depth> </size> <bndbox> <ID>1</ID> <xmin>143</xmin> <ymin>163</ymin> <xmax>332</xmax> <ymax>335</ymax> </bndbox> </Annotation>

我当前的代码:

My current code:

//create object table DataTable tableObj2 = new DataTable(); tableObj2.TableName = "object"; //add column id DataColumn objIdColumn = new DataColumn(); objIdColumn.DataType = System.Type.GetType("System.Int32"); objIdColumn.ColumnName = "ID"; tableObj2.Columns.Add(objIdColumn); tableObj2.PrimaryKey = new DataColumn[1] { objIdColumn }; DataRow row = tableObj2.NewRow(); row["ID"] = 1; //create binding box table DataTable tableBb2 = new DataTable(); tableBb2.TableName = "bndbox"; DataRow dataRow4 = tableBb2.NewRow(); //add column id and coordinates tableBb2.Columns.Add("ID", typeof(int)); tableBb2.Columns.Add("xmin", typeof(int)); tableBb2.Columns.Add("ymin", typeof(int)); tableBb2.Columns.Add("xmax", typeof(int)); tableBb2.Columns.Add("ymax", typeof(int)); //insert values dataRow4["ID"] = 1; dataRow4["xmin"] = dataGridView1.Rows[0].Cells[1].Value; dataRow4["ymin"] = dataGridView1.Rows[0].Cells[2].Value; dataRow4["xmax"] = dataGridView1.Rows[1].Cells[1].Value; dataRow4["ymax"] = dataGridView1.Rows[1].Cells[2].Value; tableBb2.Rows.Add(dataRow4); //add tables to Annotation dataset ds.Tables.Add(tableBb2); ds.Tables.Add(tableObj2); //create data relation DataRelation relation = ds.Relations.Add("relation", ds.Tables["object"].Columns["ID"], ds.Tables["bndbox"].Columns["ID"]); relation.Nested = true;

任何方向或对此的帮助,将不胜感激!

Any direction or help with this is greatly appreciated!

推荐答案

无法启用此约束,因为并非所有值都有对应的父值.

This constraint cannot be enabled as not all values have corresponding parent values.

您在此处为父表新建了一行:

You made a new row for the parent table here:

DataRow row = tableObj2.NewRow();

但是您似乎没有将其添加到父表中.我希望在尝试向引用此新行的子表中添加行之前,在某处看到这样的代码:

But you don't seem to have added it to the parent table. I would expect to see code like this somewhere before you attempt to add rows to a child table that references this new row:

tableObj2.Rows.Add(row);

因为您从未添加过此行,所以直到告诉数据集这些表相关的那一刻,您的代码才一直成功,此时它说子表中的一个或多个行在其中没有对应的父行"父表"

Because you never added this row your code succeeds all the way up to the moment you tell the DataSet these tables are related at which point it says "one or more rows in your child table don't have a corresponding parent row in the parent table"

记住;调用NewRow会给您一个新的Detached行.它必须添加到表中才能作为关系的一部分

Remember; calling NewRow gives you a new Detached row. It has to be added to a table to function as part of a relationship

在其他新闻中,如果右键单击您的项目,添加一个新项目,类别Data,键入DataSet,则您的生活会好很多.打开DataSet,右键单击设计图面,选择Add .. Datatable.通过右键单击并选择add..column来指定列.通过单击父ID中列名旁边行的灰色部分来指定您的关系,以使整个行变为蓝色,然后将蓝色行拖到另一个数据表中的子ID上并将其拖放.这将创建一个类型化的数据集.它们在智能感知中使用起来要好得多,因为它们为列等输入了属性,因此,此 row ["ID"] = 1 或 var x =(int)row ["ID"] -该行的ID属性为整数,因此它是 row.ID = 1 或 var x = row.ID -强类型,而不是字符串:)

In other news, your life will get a lot nicer if you right click your project, add a new item, category Data, type DataSet.. open the DataSet, right click the design surface, choose Add.. Datatable. Specify your columns by right clicking it and choosing add..column. Specify your relation by clicking the grey part of the row next to the column name in the parent ID so that the whole row goes blue then dragging the blue row onto the child id in the other datatable and dropping it. This makes a typed DataSet. They're a lot nicer to work with in intellisense because they have typed properties for columns etc so none of this row["ID"] = 1 or var x = (int)row["ID"] - the row will have an ID property that is integer so it's row.ID = 1 or var x = row.ID - strongly typed, not stringly typed :)

如果要创建强类型的数据集,请记住以下简单规则:如果访问.Rows或.Columns集合(或将列名放入字符串中),则可能做错了.这些集合返回基本的DataRow/DataColumn对象,这些对象将您带回到字符串类型的世界

If you do make a strongly typed DataSet remember this simple rule: if you're accessing the .Rows or .Columns collections (or putting a column name in a string) you're probably doing it wrong. These collections return base DataRow/DataColumn objects, which put you back in stringly typed world

//no dt.Rows[0] //yes dt[0] //no dt.Columns["Colname"] //yes dt.ColnameColumn //no dt[0].IsNull("Colname") //yes dt[0].IsColnameNull()

更多推荐

“无法启用此约束,因为并非所有值都有对应的父值."

本文发布于:2023-10-25 21:47:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都有   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!