Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
We are exporting data between 2 different databases for this example: Teradata -> Cloudera.

What we need is to make sure that every single row was exported.

So I create a function using C# that extract the data from Teradata and store it into a DataTable, other function that extract the data from Cloudera and store it into a DataTable.

I want to be able to compare both datatables and get if there is missing data on the extraction.

What I have tried:

I have tried merge both datatables, iterate for each row, iterate with for loop. But it take a lot of time to compare both datatables
Posted
Updated 8-Mar-16 20:29pm
v3
Comments
Mehdi Gholam 9-Mar-16 1:00am    
Are the ID's on both ends the same?

DataTables have no built-in methods for complicated comparison operations, You need to customize your solutions, may be loop over both DataTables in nested foreach-loops.
go through below snippet, see if it works
C#
static void CompareRows(DataTable table1, DataTable table2)
    {
	foreach (DataRow row1 in table1.Rows)
	{
	    foreach (DataRow row2 in table2.Rows)
	    {
		var array1 = row1.ItemArray;
		var array2 = row2.ItemArray;

		if (array1.SequenceEqual(array2))
		{
		    Console.WriteLine("Equal: {0} {1}",
			row1["col1"], row2["col1"]);
		}
		else
		{
		    Console.WriteLine("Not equal: {0} {1}",
			row1["col1"], row2["col1"]);
		}
	    }
	}
 
Share this answer
 
v2
There's numbers of ways to compare data.

The simplest way is to compare them on server's side using SQL tools, see: How to: Compare and Synchronize the Data of Two Databases[^]

The worst solution is to compare them on client's side, especially when the size of databases is large...
I'd suggest to read this: Comparing DataRows (LINQ to DataSet)[^]
Google book: LINQ For Dummies - John Paul Mueller[^]
Note: Linq solution may not be the best option, because of its performance. So, using two for-loops might be faster than Linq solution.

Decision belongs to you.
 
Share this answer
 
just count the number of rows of each datatable then compare number of rows.
 
Share this answer
 
Comments
Maciej Los 9-Mar-16 2:11am    
Very "interesting" logic...
Suppose i have 2 datatables: first one stores client's data and the second one stores their orders. There is 100 clients and only avarage 25 orders made by 4 clients. The rest of clients never placed the order.
Does the equal number of records makes them comparable?
Sazzad Hossain 9-Mar-16 2:17am    
comparing client data with orders data is absurd as well as illogical.
Maciej Los 9-Mar-16 2:38am    
...as illogical is comparing the number of rows...

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