如何删除数据集关系?

发布于 2024-10-28 03:32:26 字数 63 浏览 6 评论 0原文

我怎样才能做到这一点?我尝试过 DataSet.Relations.Clear(),但它不起作用。有什么想法吗?

How can I do that? I have tried DataSet.Relations.Clear(), but it doesn't work. any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-11-04 03:32:26

我只是从数据库设计的角度来回答。听起来您的模型有一个外键约束,可以防止在有子记录时删除父记录。根据您使用的工具和您拥有的权限(以及您希望影响数据模型完整性的程度),您也许能够删除此约束。这会在子表中留下孤立数据。您也许还可以在父级上编写一个删除触发器,该触发器首先删除所有子级。或者,您可以构建代码来查找子级,先删除它们,然后删除父级。

I'm only answering from a database design perspective. It sounds like your model has a foreign key constraint that prevents deleting a parent record when it has children. Depending on what tools you are using and what permissions you have (and how much you want to affect the integrity of the data model) you might be able to remove this constraint. This would leave orphaned data in the child table. You might also be able to write a delete trigger on the parent that goes and deletes all the children first. Alternately you can structure your code to find the children, delete them first then delete the parent.

木格 2024-11-04 03:32:26

这对我有用:

for (int i = DS.Relations.Count - 1; i >= 0; i--)
  DS.Relations.Remove(DS.Relations[i]);

按照此代码片段删除任何可能阻止从数据集中删除表的外键约束:

for (int i = DS.Tables.Count - 1; i >= 0; i--)
{
  var table = DS.Tables[i];
  for (int constraint = table.Constraints.Count - 1; constraint >= 0; constraint--)
    if (table.Constraints.CanRemove(table.Constraints[constraint]))
      table.Constraints.Remove(table.Constraints[constraint]);
}

This worked for me:

for (int i = DS.Relations.Count - 1; i >= 0; i--)
  DS.Relations.Remove(DS.Relations[i]);

Follow that up with this snippet to remove any foreign key constraints that might prevent removing tables from the dataset:

for (int i = DS.Tables.Count - 1; i >= 0; i--)
{
  var table = DS.Tables[i];
  for (int constraint = table.Constraints.Count - 1; constraint >= 0; constraint--)
    if (table.Constraints.CanRemove(table.Constraints[constraint]))
      table.Constraints.Remove(table.Constraints[constraint]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文