How do I remove a datatable from a dataset (has a bunch of relationships)
Solution 1
before you remove the table from the dataset try to clear all its constaints, something like this:
domain.Tables["TABLE_NAME"].Constraints.Clear();
should work and you should then be able to remove it from the dataset.
if you have the issue with PK Constraint which cannot be removed try this:
var myTable = domain.Tables["TABLE_NAME"];
for (int i = myTable.Constraints.Count - 1; i >= 0; --i)
{
if (myTable.Constraints[i] is System.Data.ForeignKeyConstraint)
{
myTable.Constraints.Remove(myTable.Constraints[i]);
}
}
Solution 2
Here is what worked for me:
DataTable table = dataSet.Tables["TABLE_NAME"];
while (table.ChildRelations.Count > 0)
{
var relation = table.ChildRelations[0];
dataSet.Tables[relation.ChildTable.TableName].Constraints.Remove(relation.RelationName);
dataSet.Relations.Remove(relation);
}
while (table.ParentRelations.Count > 0)
{
dataSet.Relations.Remove(table.ParentRelations[0]);
}
table.Constraints.Clear();
dataSet.Tables.Remove(table);
table.Dispose();
Related videos on Youtube
Steve0
Updated on June 04, 2022Comments
-
Steve0 almost 2 years
I am attempting to remove a datatable that was loaded into a dataset, and has been related. Here is the code I attempted.
domain.EnforceConstraints = false; if (domain.Tables["TABLE_NAME"] != null) { domain.Tables["TABLE_NAME"].ChildRelations.Clear(); domain.Tables["TABLE_NAME"].ParentRelations.Clear(); domain.Tables.Remove("TABLE_NAME"); } domain.EnforceConstraints = true;
This throws an exception at the point of removing the table, due to a foreign-key constraint existing. Unfortunately, the way the logic is I have no idea what the name of the constraint is [so I cannot hard code it].
Is there away to accomplish this in an easier fashion, or can I get some suggestions as to how to locate and remove the constraint that is causing my issue.
Thanks in advance, Steve
--------------------------ANSWER------------------------
I wasn't allowed to answer my own question so here is the solution I came up with. This code snippet now works for me. I had to travel the relation to the other table and remove the constraint from there.
if (domain.Tables["TABLE_NAME"] != null) { for (int f = domain.Tables["TABLE_NAME"].ChildRelations.Count -1; f >=0; f--) { domain.Tables["TABLE_NAME"].ChildRelations[f].ChildTable.Constraints.Remove(domain.Tables["TABLE_NAME"].ChildRelations[f].RelationName); domain.Tables["TABLE_NAME"].ChildRelations.RemoveAt(f); } domain.Tables["TABLE_NAME"].ChildRelations.Clear(); domain.Tables["TABLE_NAME"].ParentRelations.Clear(); domain.Tables["TABLE_NAME"].Constraints.Clear(); domain.Tables.Remove("TABLE_NAME"); }
-
Steve0 over 12 yearsUnfortunately, I tried this already. I get an exception "Cannot remove unique constraint 'PrimaryKey'. Remove foreign key constraint CONSTRAINT_NAME first". This is the same foreign-key that the remove call returns to me.
-
Steve0 over 12 yearsStill a no go, when I attempt to insert your snippet before the call to remove and after the call to ParentRelations.Clear() it finds only one fk constraint. Not the one that is causing the issue. Its the strangest thing.
-
Steve0 over 12 yearsGuess I am going to iterate through each table in the dataset.
-
Steve0 over 12 yearsDavide, thanks for the help, I found came upon the solution (posted in my question cause I cannot answer my own question). Neither can I upvote your assistance.
-
Steve0 about 11 yearsI'm not sure what you are getting at. Are you asking for additional information?