简体   繁体   English

如何使用ADO.NET从DAL层中的多个表返回具有一对多关系的数据

[英]How to return Data from multiple tables in DAL layer with one-to-many relationship using ADO.NET

I have one customer table and orders table with one-to-many relationship in database and my requirement is to get the corresponding list of orders for each customers. 我有一个客户表和订单表,数据库中有一对多的关系,我的要求是获得每个客户的相应订单列表。 enter image description here 在此输入图像描述

Here is the list of orders done for customerid = 1 以下是customerid = 1的订单列表

I am able to do it by multiple cycle call of database(For example - first i collected the list of customers and then for each customers i collected their orders list in corresponding listDTO and finally returned the list of customer with oders DTO to the BAL Layer. 我可以通过数据库的多次循环调用来做到这一点(例如 - 首先我收集了客户列表,然后为每个客户收集了他们在相应listDTO中的订单列表,最后将带有oders DTO的客户列表返回到BAL层。

I think this is not good to call multiple time to call database to get the data. 我认为多次调用数据库来获取数据并不好。 Is there any efficient way of doing it. 有没有有效的方法。

While you can do a join, like you said you will get back multiple records for the each order duplicating customer information, which you code will have to de-duplicate. 虽然您可以进行加入,但就像您所说的那样,您将获得每个订单重复客户信息的多条记录,您必须对其进行重复删除。

I like to use SqlDataReader's support for multiple result sets to make this easier. 我喜欢使用SqlDataReader对多个结果集的支持来使这更容易。 https://msdn.microsoft.com/en-us/library/hh223698(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/hh223698(v=vs.110).aspx

So you SQL will look something like this: 所以你的SQL看起来像这样:

--Assume that @CustomerId is a parameter (always use parameters!)
SELECT CustomerId, CustomerName
  FROM Customer
 WHERE CustomerId = @CustomerId

SELECT OrderId, OrderDate --all your order columns
  FROM Orders
 WHERE CustomerId = @CustomerId

Then in your ADO.Net code: 然后在您的ADO.Net代码中:

using (var reader = command.ExecuteDataReader())
{
    while (reader.Read())
    {
        //read first result set
    }

    reader.NextResult();

    while (reader.Read())
    {
        //read secondresult set
    }
}

Or even better, use the async versions of ExecuteDataReader, Read, NextResult :-) 甚至更好,使用ExecuteDataReader的异步版本,Read,NextResult :-)

If you are using SQL 2016 this can be done very nicely with JSON path query to return a hierarchical structure of JSON which can then be deserialized into your objects, or possibly passed through to the client. 如果您正在使用SQL 2016,那么可以使用JSON路径查询很好地完成此操作,以返回JSON的层次结构,然后可以将其反序列化到您的对象中,或者可能传递给客户端。

SELECT
            O.ORDER_ID orderId,
            O.ORDER_NAME orderName,
            O.ACTIVE_START orderDate,
            (SELECT
                OI.ORDER_ITEM_ID itemId,
                OI.NAME name,
                OI.COMMENTS comments
            FROM
                dbo.ORDER_ITEM OI
            WHERE
                O.ORDER_ID = OI.ORDER_ID
            FOR JSON PATH) items
        FROM
            dbo.[ORDER] O
        WHERE
            O.PERSON_ID = @PERSON_ID
        FOR JSON PATH;

Which would return something like 这将返回类似的东西

[
  {
    "orderId": 21,
    "orderName": "my first order",
    "orderDate": "2016-09-18T11:01:41",
    "items": [
      {
        "itemId": 41,
        "name": "pizza",
        "comments": "Extra Cheese Please"
      },
      {
        "itemId": 42,
        "name": "italian sandwich",
        "comments": "No peppers"
      }
    ]
  }
]...

There is a git project that has wrappers around ado for facilitating these kinds of calls at https://github.com/ahhsoftware/EzAdoVSSolution which might be something you'd be interested in. 有一个git项目,包含ado的包装,用于在https://github.com/ahhsoftware/EzAdoVSSolution上进行这类调用,这可能是你感兴趣的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM