如何使用C#将一个DataSet的特定列与另一个DataSet进行比较 [英] How do I compare specific columns of one DataSet to another DataSet using C#

查看:98
本文介绍了如何使用C#将一个DataSet的特定列与另一个DataSet进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试编写一个认证工具,将一个Microsoft Access .mdb的内容与另一个进行比较。到目前为止,我已经能够设置并打开两个.mdb文件的连接。我还实例化了两个DataSet对象来保存特定的表。我现在很难编码比较一个DataSet的特定列是否等于另一个。



我想要发生的是



-将表1第1列和第3列的所有内容与表2第1列和第3列的所有内容进行比较(需要帮助)



- 如果内容相同,则不做任何其他事情将表1的具体值设置为notCertified(我可以处理此部分)。



任何帮助都是这个非常感谢。

Hello,

I'm trying to code a certification tool that will compare the contents of one Microsoft Access .mdb to another. So far I have been able to set up and open a connection to the two .mdb files. I also have instantiated two DataSet objects to hold the specific tables. I'm now having difficulty in coding for comparing if the specific columns of one DataSet are equal to another.

What I want to happen is

-Compare all contents of Columns 1 and 3 of Table 1 to all contents of Columns 1 and 3 of Table 2 (NEED HELP)

-If contents are equal, do nothing else set specific value of Table 1 to notCertified (I can handle this part).

Any help in this would be greatly appreciated.

推荐答案

也许你知道这一点,但正如你所说的问题中的数据集和表:简单地说,DataSets是一个整个数据库在内存中,可能有多个表,可能还有这些表之间的关系。为了实现您的要求,您只需要两个DataTable而不是DataSet。



我理解你的问题,你只对这个问题感兴趣这两个表的记录是否相同,但如果有的话,对具体的差异不感兴趣。



Maybe you know this, but as you speak of DataSets and tables in your question: DataSets are, simply put, a whole database in memory, with potentially multiple tables and possibly relations between those tables. To achieve what you're asking for, you only need two DataTables instead of DataSets.

I understand your question in that way, that you're only interested if the records of the two tables are identical or not, but not interested in the specific differences if there are any.

bool CompareInstalledToBaseline(OleDbConnection connection)
{
    // change the column-names appropriately:
    string query1 = "SELECT col1, col3 FROM InstalledConfigItem ORDER BY col1, col3;";
    string query2 = "SELECT col1, col3 FROM BaselineConfigItem ORDER BY col1, col3;";

    using (var installedDBAdapter = new OleDbDataAdapter(query1, connection))
    using (var baselineDBAdapter = new OleDbDataAdapter(query2, connection))
    using (var installedTable = new DataTable())
    using (var baselineTable = new DataTable())
    {
        installedDBAdapter.Fill(installedTable);
        baselineDBAdapter.Fill(baselineTable);

        int[] columnsToCompare = new int[] { 0, 1 };

        return CompareTables(installedTable, baselineTable, columnsToCompare);
    }
}

bool CompareTables(DataTable table1, DataTable table2, IEnumerable<int> columnsToCompare) 
{
   if (table1.Rows.Count != table2.Rows.Count)
      return false;

   for (int rowIndex = 0; rowIndex < table1.Rows.Count; rowIndex++)
   {
       foreach (int colIndex in columnsToCompare)
       {
           if (!table1.Rows[rowIndex][colIndex].Equals(table2.Rows[rowIndex][colIndex]))
           {
               return false;
           }
       }
   }

   return true;
}





您可以先通过查询两个表的记录数来优化此过程。如果已经有所不同,您将不再需要加载和比较记录。



如果您对代码有疑问,请询问。



You could optimize this process by querying the record-count of both tables first. If that's already different, you won't have to load and compare the records any more.

If you have questions regarding the code please ask.


这篇关于如何使用C#将一个DataSet的特定列与另一个DataSet进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆