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");
}
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]);
}
}
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();
Your Answer is right But tables are not deleted from Dataset .. Still it remains if user wants to create a same table name .. then it will show Table exists ..
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");
}
Grave-digging, but just hit this and needed to remove several tables which also had child tables, so cooked up a DataSet extension method:
public static void RemoveTable(this DataSet dataSet, string tableName)
{
if (!dataSet.Tables.Contains(tableName))
{
throw new ArgumentException($"Table {tableName} doesn't exist in DataSet");
}
var relations = new DataRelation[dataSet.Tables[tableName].ChildRelations.Count];
dataSet.Tables[tableName].ChildRelations.CopyTo(relations, 0);
foreach (DataRelation relation in relations)
{
RemoveTable(dataSet, relation.ChildTable.TableName);
}
dataSet.Tables[tableName].ChildRelations.Clear();
dataSet.Tables[tableName].ParentRelations.Clear();
dataSet.Tables[tableName].Constraints.Clear();
dataSet.Tables.Remove(tableName);
}
精彩评论