Click here to Skip to main content
16,011,120 members

Comments by sivared (Top 9 by date)

sivared 29-Jul-13 0:46am View    
Yes Rohan. I used the same code.I am beginner in C#. I am not expert. But it is not working for tables more than 32 columns.
sivared 29-Jul-13 0:09am View    
public DataTable CompareTables(DataTable first, DataTable second)
{
first.TableName = "FirstTable";

second.TableName = "SecondTable";

//Create Empty Table

DataTable table = new DataTable("Difference");

try
{
//Must use a Dataset to make use of a DataRelation object
using (DataSet ds = new DataSet())
{
ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });

//Get Columns for DataRelation

DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];


for (int i = 0; i < firstcolumns.Length; i++)
{

firstcolumns[i] = ds.Tables[0].Columns[i];

}


DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

for (int i = 0; i < secondcolumns.Length; i++)
{

secondcolumns[i] = ds.Tables[1].Columns[i];

}


//Create DataRelation

DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

ds.Relations.Add(r);

//Create columns for return table

for (int i = 0; i < first.Columns.Count; i++)
{

table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);

}


//If First Row not in Second, Add to return table.

table.BeginLoadData();



foreach (DataRow parentrow in ds.Tables[0].Rows)
{

DataRow[] childrows = parentrow.GetChildRows(r);

if (childrows == null || childrows.Length == 0)

table.LoadDataRow(parentrow.ItemArray, true);

}

table.EndLoadData();

}
}
catch (Exception ex)
{

throw ex;

}

return table;
}

It is working for only tables with less than 32 columns only. If a table has more than 32 columns, it is not working. Please improve this solution to work for any number of columns. Thanks in advance.
sivared 29-Jul-13 0:04am View    
Thanks. But I am using the following code. Can you help me on this. If possible please provide me C# code. for looping through data tables.
public DataTable compareanotherlogic(DataTable FirstDatatable,DataTable SecondTable)
{

DataTable InsertTable = new DataTable("FinalInserts");

for (int i = 0; i < FirstDatatable.Columns.Count; i++)
{
InsertTable.Columns.Add(FirstDatatable.Columns[i].ColumnName, FirstDatatable.Columns[i].DataType);
}
var table3 = FirstDatatable.Copy();
table3.AcceptChanges();
table3.Merge(SecondTable);

var distinctRows = from row in table3.AsEnumerable()
where row.RowState != DataRowState.Modified
select row;

var distintTable = distinctRows.CopyToDataTable();

return distintTable;
}
sivared 24-Jul-13 21:39pm View    
If I have two data tables reading from xsd.Each table is same and is getting from different databases in whcih each table has 30 columns, and 28000 records. Tables differ in data in most of the columns. How can I compare the two datatables which I am reading from xsd. and create the final table with all the different records with both inserts and updates into excel sheet. Can I identify them uniquely thiat is updated record and inserted record.
sivared 24-Jul-13 21:35pm View    
Thanks. I will try this one.