如何在 C# 中过滤和组合 2 个数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2267/
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-07-31 13:02:33  来源:igfitidea点击:

How to filter and combine 2 datasets in C#

提问by saalon

I am building a web page to show a customer what software they purchased and to give them a link to download said software. Unfortunately, the data on what was purchased and the download information are in separate databases so I can't just take care of it with joins in an SQL query.

我正在构建一个网页,向客户展示他们购买的软件,并为他们提供下载所述软件的链接。不幸的是,有关购买的数据和下载信息位于不同的数据库中,因此我不能仅通过 SQL 查询中的连接来处理它。

The common item is SKU. I'll be pulling a list of SKUs from the customer purchases database and on the download table is a comma delineated list of SKUs associated with that download. My intention, at the moment, is to create from this one datatable to populate a GridView.

常见的项目是SKU。我将从客户购买数据库中提取 SKU 列表,下载表上是与该下载关联的 SKU 逗号分隔列表。目前,我的目的是从这个数据表创建以填充GridView.

Any suggestions on how to do this efficiently would be appreciated. If it helps, I can pretty easily pull back the data as a DataSetor a DataReader, if either one would be better for this purpose.

任何关于如何有效地做到这一点的建议将不胜感激。如果有帮助,我可以很容易地将数据作为 aDataSet或 a拉回DataReader,如果其中任何一个更适合此目的。

回答by Vaibhav

I am thinking off the top of my head here. If you load both as Data Tables in the same Data Sets, and define a relation between the two over SKU, and then run a query on the Data Set which produces the desired result.

我在想这里。如果将两者作为数据表加载到同一数据集中,并通过 SKU 定义两者之间的关系,然后对产生所需结果的数据集运行查询。

回答by Yaakov Ellis

As long as the two databases are on the same physical server (assuming MSSQL) and the username/password being used in the connection string has rights to both DBs, then you should be able to perform a join across the two databases. Example:

只要两个数据库在同一个物理服务器上(假设 MSSQL)并且连接字符串中使用的用户名/密码对两个数据库都有权限,那么您应该能够跨两个数据库执行连接。例子:

select p.Date,
       p.Amount,
       d.SoftwareName,
       d.DownloadLink
from   PurchaseDB.dbo.Purchases as p
join   ProductDB.dbo.Products as d on d.sku = p.sku
where  p.UserID = 12345

回答by Brett Veenstra

Why not create a Domain object based approach to this problem:

为什么不创建一个基于域对象的方法来解决这个问题:

public class CustomerDownloadInfo
{
    private string sku;
    private readonly ICustomer customer;

    public CustomerDownloadInfo(ICustomer Customer){
        customer = Customer;
    }

    public void AttachSku(string Sku){
        sku = Sku;
    }

    public string Sku{
        get { return sku; }
    }

    public string Link{
        get{    
            // etc... etc...          
        }
    }
}

There are a million variations on this, but once you aggregate this information, wouldn't it be easier to present?

这方面有一百万种变化,但是一旦您汇总了这些信息,会不会更容易呈现?