C# 如何在同一数据集中的表之间建立关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1992782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make relation between tables which are in same dataset?
提问by Ashish
I have one dataset in which there are 40 tables. Now I want to make a relation between these tables and show important data in grid. How do i do this?
我有一个数据集,其中有 40 个表。现在我想在这些表之间建立关系并在网格中显示重要数据。我该怎么做呢?
回答by Meligy
Did you try something like:
你有没有尝试过:
ds.Relations.Add("Products_Category",
ds.Tables("Categories").Columns("CategoryID"),
ds.Tables("Products").Columns("CategoryID"));
回答by Michael Petrotta
If you're creating a typed dataset, it's easiest to create the relations in Visual Studio's dataset designer. Just right-click on the table in the designer, select Add->Relation, and specify the relation.
如果要创建类型化数据集,最容易在 Visual Studio 的数据集设计器中创建关系。只需在设计器中的表上单击鼠标右键,选择添加->关系,然后指定关系。
If you need to specify the relation in code, you can do it like this:
如果你需要在代码中指定关系,你可以这样做:
dataSet.Relations.Add(dataSet.Tables["Customers"].Columns["customerId"],
dataSet.Tables["Orders"].Columns["customerId"]);
Read all about it in MSDN here.
在此处阅读 MSDN 中的所有相关信息。
回答by DOK
That is a large number of DataTables
to have in a DataSet
.
这是DataTables
一个DataSet
.
The first thing I would consider would be to reduce the number of DataTables
(and eliminate the need for Relations
) by populating the DataTables
with queries that JOIN the database tables. For example, instead of having one DataTable
for Product Category and another for Product Detail, it might be possible to combine the data for both database tables into one DataTable
. Similarly, for Customer, Customer Address and Customer Phone, retrieve all of the data in one DataTable by using one query that does a JOIN on all three database tables.
我会考虑的第一件事是通过填充JOIN 数据库表的查询来减少数量DataTables
(并消除对 的需求Relations
)DataTables
。例如,DataTable
可以将两个数据库表的数据合并为一个,而不是将一个用于产品类别而另一个用于产品详细信息DataTable
。同样,对于 Customer、Customer Address 和 Customer Phone,通过使用对所有三个数据库表执行 JOIN 的查询来检索一个 DataTable 中的所有数据。
Once you have minimized the number of DataTables
in the DataSet
, you can add Relations
between DataTables
if they have matching columns (even if the columns have different names). For example, there might be an Orders DataTable
with a CustomerID column that matches the ID column in the Customers DataTable
.
最小化 中的数量后DataTables
,如果它们具有匹配的列(即使列具有不同的名称)DataSet
,则可以Relations
在DataTables
它们之间添加。例如,可能有一个 OrdersDataTable
的 CustomerID 列与 Customers 中的 ID 列匹配DataTable
。
Here is the code to add a Relation
to the DataSet
for that situation. Assume that we have a DataSet
dst containing two DataTables
Customers and Orders.
这里是一个添加代码Relation
到DataSet
了这种情况。假设我们有一个DataSet
包含两个DataTables
客户和订单的dst 。
DataColumn customerColumn, orderColumn;
customerColumn = dst.Tables["Customers"].Columns["ID"];
orderColumn = dst.Tables["Orders"].Columns["CustomerID"];
DataRelation dr = new DataRelation("CustomerOrders", customerColumn, orderColumn);
dst.Relations.Add(dr);
回答by viewer
ds.Relations.Add("Products_Category",
ds.Tables("Categories").Columns("CategoryID"),
ds.Tables("Products").Columns("CategoryID"));
回答by ahmad salimi
private void CreateRelation()
{
// Get the DataColumn objects from two DataTable objects
// in a DataSet. Code to get the DataSet not shown here.
DataColumn parentColumn =
DataSet1.Tables["Customers"].Columns["CustID"];
DataColumn childColumn =
DataSet1.Tables["Orders"].Columns["CustID"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders",
parentColumn, childColumn);
// Add the relation to the DataSet.
DataSet1.Relations.Add(relCustOrder);
}