Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Garth J Lancaster 14-Apr-15 20:14pm    
what exactly are you stuck on ? you'd be better off posting the code that you have so far, than asking people here to provide a full swag of code for you - its not done/how CP works anyway.. Update your question with what you've got/how you've tried, and people will be more likely to help
Member 11579826 14-Apr-15 20:34pm    
Ok Garth,

What I have is:

OleDataAdapter installedDBAdapter = new OleDataAdapter("",installedDBConnection);
OleDataAdapter baselineDBAdapter = new OleDataAdapter("",installedDBConnection);

//Open up a connection to the databases
code for opening connection

DataSet installedDS = new DataSet();
DataSet baselineDS = new DataSet();

installedDBAdpter.Fill(installedDS, "InstalledConfigItem");
baselineDBAdapter.Fill(baselineDS, "BaselineConfigItem");

DataColumn[] installedConfigItemFirstColumn = new DataColumn[installedDS.Tables[0].Rows.Count];

DataColumn[] baselineConfigItem = new DataColumn[baselineDS.Tables[0].Rows.Count];

I want to compare columns 1 and 3 of installedDS to columns 1 and 3 of baselineDS.

Hopefully this clarifies things.

1 solution

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.

C#
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.
 
Share this answer
 
v4
Comments
Member 11579826 15-Apr-15 14:17pm    
I'm still testing the overall project but your code suggestion helped make my project work. Thank You
Sascha Lefèvre 15-Apr-15 14:54pm    
You're welcome, glad I could help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900