我试图删除加载到数据集中的数据表,并且已经相关.这是我尝试的代码.
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;
Run Code Online (Sandbox Code Playgroud)
由于存在外键约束,这会在删除表时抛出异常.不幸的是,逻辑的方式我不知道约束的名称是什么[所以我不能硬编码].
有没有以更容易的方式完成此任务,或者我可以获得一些关于如何找到并删除导致我的问题的约束的建议.
先谢谢,史蒂夫
- - - - - - - - - - - - - 回答 - - - - - - - - - - - - -
我不被允许回答我自己的问题所以这是我提出的解决方案.此代码段现在适用于我.我必须将关系传递到另一个表并从那里删除约束.
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");
}
Run Code Online (Sandbox Code Playgroud)