简体   繁体   English

如何比较两个数据表

[英]How do I compare two datatables

I have a datatable that will consist of 72 columns. 我有一个数据表,它将包含72列。

I will download it in the excel sheet using VSTO, which works fine. 我将使用VSTO将其下载到excel工作表中,效果很好。

Now the user will change either one of these rows or all of these rows and will also insert a fresh row. 现在,用户将更改这些行之一或所有这些行,还将插入一个新行。

Considering the datatable downloaded first to be dtA, and the one that has been modified in the excel sheet to be dtB. 考虑首先下载的数据表是dtA,而在excel工作表中已修改的数据表是dtB。

I want to compare dtA and dtB. 我想比较dtA和dtB。

I need to find out all the rows in dtB that do not exist in dtA. 我需要找出dtB中不存在的所有dtB行。

I cant put foreach loop for each and every single row and evaluate as its a very untidy way of coding. 我不能为每一行放foreach循环,并且将其评估为一种非常不整洁的编码方式。

What is a better way to do this? 有什么更好的方法可以做到这一点?

I did this way, 我是这样的

    DataTable dtA = new DataTable();
    dtA.Columns.Add("ENo");
    dtA.Columns.Add("ENo1");
    dtA.Columns.Add("ENo2");
    dtA.Columns.Add("ENo3");
    dtA.Columns.Add("ENo4");

    for (int i = 0; i < 5; i++)
    {
        DataRow dr = dtA.NewRow();
        dr[0] = "Part 0 " + i.ToString();
        dr[1] = "Part 1 " + i.ToString();
        dr[2] = "Part 2 " + i.ToString();
        dr[3] = "Part 3 " + i.ToString();
        dr[4] = "Part 4 " + i.ToString();
        dtA.Rows.Add(dr);
    }

    DataTable dtB = new DataTable();
    dtB.Columns.Add("ENo");
    dtB.Columns.Add("ENo1");
    dtB.Columns.Add("ENo2");
    dtB.Columns.Add("ENo3");
    dtB.Columns.Add("ENo4");

    for (int i = 5; i < 10; i++)
    {
        DataRow dr = dtB.NewRow();
        dr[0] = "Part 0 " + i.ToString();
        dr[1] = "Part 1 " + i.ToString();
        dr[2] = "Part 2 " + i.ToString();
        dr[3] = "Part 3 " + i.ToString();
        dr[4] = "Part 4 " + i.ToString();
        dtB.Rows.Add(dr);
    }

    Response.Write("\n");
    Response.Write("dt A");
    Response.Write("\n");

    for (int i = 0; i < dtA.Rows.Count; i++)
    {
        Response.Write(dtA.Rows[i][i].ToString());
        Response.Write("\n");
    }

    Response.Write("\n");
    Response.Write("dt B");
    Response.Write("\n");
    for (int i = 0; i < dtB.Rows.Count; i++)
    {
        Response.Write(dtB.Rows[i][i].ToString());
        Response.Write("\n");
    }

    var VarA = dtA.AsEnumerable();
    var varB = dtA.AsEnumerable();

    var diff = VarA.Except(varB);
    Response.Write("except");
    foreach (var n in diff)
    {
        Response.Write(n.Table.Rows[0].ToString());

    }

But I do not know what to use in the foreach var, What should I use pls? 但是我不知道在foreach var中使用什么,我应该使用pls?

SELECT id FROM dtB WHERE id NOT IN (SELECT id FROM dtA)

Perhaps LINQ for DataSets (this just represents the fact that DataSets support LINQ and is part of the .NET 3.5+ framework) may be an option. 也许可以选择LINQ for DataSet (这仅表示DataSet支持LINQ并且是.NET 3.5+框架的一部分)。 In particlar, check out the ExceptRows Set Pattern which just uses IEnumerable.Except . 特别是,检查仅使用IEnumerable.ExceptExceptRows设置模式 It is easiest if the row contains some "unique ID", but it should be sufficient to cover cases where the row itself is uniquely missing and/or detect if any row is modified. 如果该行包含一些“唯一ID”,这是最简单的方法,但它足以涵盖该行本身唯一丢失和/或检测是否有任何行被修改的情况。

There is also QueryADataSet -- the only product I know to "support SQL syntax on Data[Set|Table]" stuff (although it could be [weakly] argued that LINQ provides "SQL syntax"). 还有QueryADataSet -我知道的唯一产品“在Data [Set | Table]上支持SQL语法”(尽管可能[弱]认为LINQ提供了“ SQL语法”)。 Development licenses are $200 a pop, but it might be worth it, depending. 开发许可证的价格是200美元,但这可能是值得的,具体取决于。 (I have no affiliation with, nor have I tried the product; I stumbled upon it when trying to find a solution to a similar problem but ultimately just changed my approach.) (我没有从属关系,也没有尝试过该产品;在尝试找到类似问题的解决方案时偶然发现了它,但最终只是改变了我的方法。)

Happy coding. 快乐的编码。

SELECT dtB.* FROM dtB LEFT JOIN dtA ON dtB.id=dtA.id /* AND ...  */
WHERE dtA.id IS NULL

You'll only see rows from dtB that don't have a match in dtA. 您只会看到dtB中与dtA不匹配的行。

You can add whatever columns that need to be identical to count as the same row in the JOIN condition. 您可以添加任何需要相同的列才能在JOIN条件中计为同一行。

Depending on the database, there could be simpler ways. 根据数据库,可能有更简单的方法。 For example, Oracle has the MINUS keyword, to remove just entirely identical rows; 例如,Oracle具有MINUS关键字,以仅删除完全相同的行。 rows that have been modified will still appear in the result. 已修改的行仍将出现在结果中。

Alternatively: you can export both Excel files as text (CSV, tab delimited, ...), provided they're in the same row order, and use the command line utility diff to see what's changed. 或者:您可以将两个Excel文件都导出为文本(CSV,制表符分隔,...),前提是它们的行顺序相同,并可以使用命令行实用程序diff查看更改的内容。

There could be a library available for your programming language that implements the diff algorithm, so you can do it all in memory. 可能存在一种可用于您的编程语言的库,该库可实现diff算法,因此您可以在内存中完成所有操作。

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

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