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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:29:40  来源:igfitidea点击:

How to make relation between tables which are in same dataset?

c#asp.netado.net

提问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 DataTablesto 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 DataTableswith queries that JOIN the database tables. For example, instead of having one DataTablefor 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(并消除对 的需求RelationsDataTables。例如,DataTable可以将两个数据库表的数据合并为一个,而不是将一个用于产品类别而另一个用于产品详细信息DataTable。同样,对于 Customer、Customer Address 和 Customer Phone,通过使用对所有三个数据库表执行 JOIN 的查询来检索一个 DataTable 中的所有数据。

Once you have minimized the number of DataTablesin the DataSet, you can add Relationsbetween DataTablesif they have matching columns (even if the columns have different names). For example, there might be an Orders DataTablewith a CustomerID column that matches the ID column in the Customers DataTable.

最小化 中的数量后DataTables,如果它们具有匹配的列(即使列具有不同的名称)DataSet,则可以RelationsDataTables它们之间添加。例如,可能有一个 OrdersDataTable的 CustomerID 列与 Customers 中的 ID 列匹配DataTable

Here is the code to add a Relationto the DataSetfor that situation. Assume that we have a DataSetdst containing two DataTablesCustomers and Orders.

这里是一个添加代码RelationDataSet了这种情况。假设我们有一个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);
    }