将关系数据读入数据集的最简单方法

发布于 2024-10-03 03:12:08 字数 232 浏览 3 评论 0原文

我有两个表,其关系在第三个 n:m 表中表示。我们称它们为“个人”、“工作组”和“个人工作组”。我有一个查询返回多个人员,例如“选择具有特定名称的所有人员”。此外,我想要所有相关的工作组。最后,我希望有一个填充数据的数据集,以便我可以迭代客户并获取他们的相关订单。

有人有一个很好的例子来说明将这种结构放入数据集中的最简单方法吗?我更愿意用纯代码来完成它,没有任何类型的记录集,图形编辑器,......

欢呼, 阿希姆

I have two tables whichs relation is expressed in a third n:m table. Lets call them Person, Workgroup and PersonWorkgroups. I have query which returns multiple Persons like "select all persons having a certain name". In addition I want to have all related Workgroups. At the end I would like to have a DataSet filled with the data in way that I can iterate over the customers and get their related Orders.

Has somebody a good example for the simplest way to get such a structure into a DataSet? I would prefere to do it in pure code without any typed recordsets, graphical editors, ...

cheers,
Achim

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

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

发布评论

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

评论(3

翻身的咸鱼 2024-10-10 03:12:08

我真的建议使用 LINQ to XML,只是因为我认为它是一个使用关系数据库的绝佳方式。

如果您只想运行查询并获取DataSet,您不能在查询中进行联合吗,如下所示:select all personinfo, orderinfo from people P, Orders O where P .personno = O.orderno 具有特定名称

然后,您将获得一个DataSet,其中每个订单一行,并且链接到该订单的人员信息附加到每一行。

I would really suggest using LINQ to XML, just because I think it's an awesome way of working with a relational database.

If you just want to run the query and get a DataSet, can't you just make a union inside your query like this : select all personinfo, orderinfo from persons P, orders O where P.personno = O.orderno having a certain name?

Then you will get a DataSet with one line per order, and the person info linked to that order attached to each line.

羁绊已千年 2024-10-10 03:12:08

不确定您指的是数据表还是数据集。如果您执行以下查询:

  select customer.customerid, customername, orderid, orderdate, orderamount
  from customer inner join orders
  on customer.customerid = orders.customerid and customer.customerid = {your id here passed as a parameter}

通过 Command 对象,结果可以放置在 DataTable 中。如果在一个命令中执行以下两个查询,则可以将结果放入 DataSet 中以供客户端处理。您可以使用 ADO.NET 定义客户端关系。

  select customerid, customername from customer;
  select orderid, orderdate, customerid, orderamount from orders

返回两个表,您可以使用 DataAdapter.Fill() 填充数据集。

DataTable 方法可能比 DataSet 方法“更简单”,但这实际上取决于您的应用程序的要求。阅读 Command、DataAdapter、DataTable 和 DataSet 对象。使用 ADO.NET,您可以通过多种方式解决问题。

由于问题也被标记为 TSQL:您也可以创建一个存储过程并通过 ADO 命令执行它:

     create proc getCustomerOrders
     @customerid int
     as
     begin
     select
     customer.customerid, customername, orderid, orderdate, orderamount
     from customer inner join orders
      on customer.customerid = orders.customerid and customer.customerid = @customerid
     end

Not sure whether you mean DataTable or Dataset. If you execute the following query:

  select customer.customerid, customername, orderid, orderdate, orderamount
  from customer inner join orders
  on customer.customerid = orders.customerid and customer.customerid = {your id here passed as a parameter}

via a Command object, the results can be placed in a DataTable. If you execute the following two queries in one command, the result can be placed in a DataSet for client-side processing. You can define relations client-side using ADO.NET.

  select customerid, customername from customer;
  select orderid, orderdate, customerid, orderamount from orders

two tables are returned and you use DataAdapter.Fill() to populate the Dataset.

The DataTable approach could be "simpler" than the DataSet approach but it really depends on your application's requirements. Read up on the Command, DataAdapter, DataTable, and DataSet objects. With ADO.NET you can solve the problem in a variety of ways.

Since the question is also tagged TSQL: you could create a stored procedure and execute that via the ADO Command too:

     create proc getCustomerOrders
     @customerid int
     as
     begin
     select
     customer.customerid, customername, orderid, orderdate, orderamount
     from customer inner join orders
      on customer.customerid = orders.customerid and customer.customerid = @customerid
     end
離殇 2024-10-10 03:12:08

Tims 的答案接近我的要求,但表之间的关系必须在 C# 代码中重建。在经典的 ADO 中,可以用特殊的 SQL 方言定义此类层次结构。对于 Ado.Net 和 T-SQL 来说,类似的事情是不可能的。所以我的问题的答案是:这是不可能的!

Tims answer is close to my requirement, but the relation between the tables has to be reconstructed in the C# code. In classic ADO it was possible to define such hierarchies in a special SQL dialect. Something like that is not possible with Ado.Net and T-SQL. So the answer to my question is: It's just not possible!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文