如何在数据集中的表之间添加关系 [英] how to add a relationship between tables in the dataset

查看:195
本文介绍了如何在数据集中的表之间添加关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来获取同一数据集中的表之间的关系,但运行以下代码时,我遇到错误说,这些列目前没有唯一的值

I have the following code to get the get the relationship between the tables in the same dataset , but when run the following code i encounter with error saying , these columns currently dont have unique values

       DataResultSetDataSet dataset = resultSet as DataResultSetDataSet;
        System.Data.DataSet menuDataSet = new System.Data.DataSet();
        menuDataSet = dataset.Set;
        menuDataSet.DataSetName = "Menus";
        menuDataSet.Tables[0].TableName = "Menu";
        DataRelation relation = new DataRelation("ParentChild", menuDataSet.Tables["Menu"].Columns["MenuID"], menuDataSet.Tables["Menu"].Columns["ParentID"], true);
        relation.Nested = true;
        menuDataSet.Relations.Add(relation);

        menuXml= menuDataSet.GetXml();
        }


推荐答案

如果您的代码不打字错误(即,您不想尝试将表格菜单与自己相关联),请检查两列中的值(MenuID和ParentID)。不知道你的表是如何设置的,我怀疑ParentID重复的值,这就是为什么你得到你的错误。

If your code isn't a typo (i.e., you're not trying to relate the table Menu to itself), check the values in the two columns (MenuID and ParentID). Without knowing exactly how your table(s) are set up, I would suspect that ParentID has repeated values, which is why you're getting the error you are.

这篇关于如何在数据集中的表之间添加关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆