从 C# 代码中与 Table1 的关系获取 Table2 数据
我的数据集中有两个表,每个表的 ID 字段相同。 我有两个表之间的关系。 如何在 C# 代码中从 Table2 中提取与 Table1 上的信息相关的信息?
我尝试使用新的 DataRow 并使用 GetChildRow 分配它,但由于某种原因我似乎无法使其工作。
另外,我知道这个问题可能没有那么丰富的信息,请告诉我,我会尽力提供更多说明。
I have two tables in a DataSet where the ID field on each is the same. I have a Relation between the two tables. How do I, in C# code, pull the info from Table2 that relates to the info on Table1?
I have tried using a new DataRow and assigning it by using GetChildRow, but for some reason I cannot seem to make it work.
Also, I understand this question may not be that informative, let me know and I will try to provide more clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的数据集是强类型的(即从 .xsd 文件生成),答案会有所不同(并且明显更容易)。 我假设下面的情况不是这样,但如果是的话请说出来。
对于通用 DataSet 对象,答案很大程度上取决于您所掌握的内容。 如果您只有一个 ID,那么在相关数据表的选择中使用该 ID 可能是最简单的方法。 这适用于任一(或两个)表,因为它将返回包含您要查找的信息的 DataRows 数组。
如果您有一个父 DataRow(而且您似乎很可能有),那么使用的最佳方法取决于关系,即哪个是父级。 如果 Table1 是您的父级,并且您想要导航到相关的 Table2 子行,则您需要查找 GetChildRow(最好尽可能具体地告诉它要遵循哪个关系)。 如果 Table2 是父级,并且您要从 Table1 DataRow 导航到 Table2 中的父级,则您将需要使用 GetParentRow(同样,在标识关系时尽可能具体 — 如果方便的话,请使用关系对象)。
The answer is different (and significantly easier) if your DataSet is strongly-typed (i.e. generated from a .xsd file). I'll assume that's not the case below, but if it is speak up.
For generic DataSet objects, the answer largely depends on what you have hold of to start. If you have simply an ID, then it's probably simplest to use the ID in a select on the relevant DataTable. This will work for either (or both) tables as it will return an array of DataRows with the information you're looking for.
If you have a parent DataRow (and it seems likely that you do), then the best method to use depends on the relationship—i.e. which is the parent. If Table1 is your parent and you want to navigate to relevant Table2 child rows, you're looking for GetChildRow (best to be as specific as you can in telling it which relation to follow). If Table2 is the parent and you're navigating from a Table1 DataRow to the parent in Table2, you'll want to use GetParentRow (again, be as specific in identifying the relation as you can—use the relation object if you have it handy).
在强类型 DataSet 中,每个 DataRow 对象都针对每个关系具有特定的方法或属性。 如果您有一个父 DataRow 并且想要导航到 Table2 中的子级,则它将是一个复数方法(即“row.GetTable2Rows()”)。 如果您有一个子级并且想要导航到父级,则它将是一个单一属性(“row.Table2Row”)。
In a strongly-typed DataSet, each DataRow object will have specific methods or properties for each relationship. If you have a parent DataRow and want to navigate to children in Table2, it will be a pluralized method (i.e. "row.GetTable2Rows()"). If you have a child and want to navigate to the parent, it will be a singular property ("row.Table2Row").
这就是我所做的。 如果有更快的方法,请告诉我这是否是最好的。 我创建了一个与表 2 完全相同的新表,并编写了以下代码:
然后我可以调用记录为零处的一个表并获取我需要的名称。 确实有效,但是有没有更有效的方法呢?
So here is what I did. Let me know if this is best if or there is something faster. I created a new table that is an exact replica of Table 2 and wrote this code:
Then I can call the one table at record zero and get the name I need. It works, but is there a more efficent way?
评论对于这个回复来说不够长,所以我会做出另一个答案:
没有必要使用新表。 只需使用行对象数组并从那里开始。 假设您的 DataSet 命名空间是“EventData”,我将对其进行一些分解,这样它就不会全部在一行上。
然后,您可以根据需要对“eventGroup”数组进行操作,包括使用 eventGroup[0] 获取第一个元素。
A comment wasn't long enough for this response so I'll make it another answer:
There's no need to use a new table. Just use an array of row objects and go from there. Say your DataSet namespace is "EventData" and I'll break it down a bit so it isn't all on one line.
Then you can act on the "eventGroup" array as you would like including getting at the first element with eventGroup[0].