[英]C# How create primary key to existing table column
I would like to ask for your help. 我想请你帮忙。 The problem is that I am trying to relate two tables so I need to create primary keys on the tables each "id" columns but I am getting an error on this line 问题是我试图关联两个表,所以我需要在每个“ id”列的表上创建主键,但是此行出现错误
ds.Relations.Add(dr);
Error: 错误:
This constraint cannot be enabled as not all values have corresponding parent values. 无法启用此约束,因为并非所有值都有对应的父值。
(original error in Spanish = No se puede habilitar esta restricción ya que todos los valores no tienen los valores primarios correspondientes. ) (西班牙语中的原始错误= 没有适当的名称,没有任何相关信息。 )
What am I doing wrong? 我究竟做错了什么?
OdbcDataAdapter sdata = new OdbcDataAdapter("SELECT * FROM componentes WHERE nombre_componente ILIKE '%" + buscar + "%'", conn);
OdbcDataAdapter sdata2 = new OdbcDataAdapter("SELECT * FROM proveedores WHERE nombre_empresa ILIKE '%" + buscar + "%'", conn);
DataSet ds = new DataSet();
DataTable dtbl = new DataTable("dtbl");
DataTable dtbl2 = new DataTable("dtbl2");
sdata.Fill(dtbl);
sdata2.Fill(dtbl2);
ds.Tables.Add(dtbl);
ds.Tables.Add(dtbl2);
dtbl.PrimaryKey = new DataColumn[] { dtbl.Columns["id"] };
dtbl2.PrimaryKey = new DataColumn[] { dtbl2.Columns["id"] };
DataRelation dr = new DataRelation("provcomp",
ds.Tables["dtbl"].Columns["id_prov_comp"],
ds.Tables["dtbl2"].Columns["id"]);
ds.Relations.Add(dr);
ds.AcceptChanges();
Thanks in advance. 提前致谢。
The problem here is your data. 这里的问题是您的数据。 You're adding a primary key to each table and then attempting to link them with a foreign key. 您正在向每个表添加主键,然后尝试将它们与外键链接。
However, from the error message you are receiving, it sounds like you are attempting to link dtbl to an id from dtbl2 which does not exist. 但是,从收到的错误消息中,听起来您正在尝试将dtbl链接到dtbl2中不存在的ID。
I would try to do a SQL query to find out where your problem records lie and then choose to either add a "parent", change the parent or delete the record. 我会尝试执行一个SQL查询来找出问题记录所在的位置,然后选择添加“父项”,更改父项或删除记录。
SELECT * FROM componentes WHERE id_prov_comp NOT IN (SELECT ID FROM proveedores )
The primary keys are ok - you already successfully created them with the following lines: 主键没问题-您已经使用以下行成功创建了它们:
dtbl.PrimaryKey = new DataColumn[] { dtbl.Columns["id"] };
dtbl2.PrimaryKey = new DataColumn[] { dtbl2.Columns["id"] };
The problem is (as indicated by the exception) in the DataRelation
. 问题(如异常所示)在DataRelation
。 DataRelation
is the equivalent of the database FK (foreign key). DataRelation
等效于数据库FK(外键)。 The signature of the used constructor looks like this: 使用的构造函数的签名如下所示:
public DataRelation(
string relationName,
DataColumn parentColumn,
DataColumn childColumn
)
I think what you did wrong is specifying the wrong columns - parentColumn
should point to the table being referenced while childColumn
- the table referencing it. 我认为你做错了什么是指定了错误的列- parentColumn
应指向表中同时引用childColumn
-表引用它。
Simple change it to: 只需将其更改为:
DataRelation dr = new DataRelation("provcomp",
ds.Tables["dtbl2"].Columns["id"],
ds.Tables["dtbl"].Columns["id_prov_comp"]);
and if they are really related correctly, the problem should be solved. 如果它们确实正确相关,则应该解决该问题。
Edit: In case the parent table does not contain all the keys from the child table, you might consider using this constructor overload and pass createConstraints = false
, which will prevent the exception, but note that some child records will not to find their parent record. 编辑:如果父表未包含子表中的所有键,则可以考虑使用此构造函数重载并传递createConstraints = false
,这将防止异常,但请注意,某些子记录将无法找到其父记录。
DataRelation dr = new DataRelation("provcomp",
ds.Tables["dtbl2"].Columns["id"],
ds.Tables["dtbl"].Columns["id_prov_comp"],
false);
The parent column (that is primary key) must come before the child column (that is foreign key). 父列(即主键)必须位于子列(即外键)之前。 Try this: 尝试这个:
DataRelation dr = new DataRelation("provcomp",
ds.Tables["dtbl2"].Columns["id"],
ds.Tables["dtbl"].Columns["id_prov_comp"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.