How to merge two datatables with PrimaryKeys?

1

I have two data tables and want merge columns and create as third table.

//Given
dt1 {A(PK),B,C,D}
dt2 {a(PK),b,c,d}
//Want
dtResult {A,B,C,D,a,b,c,d}

Do not want to lose ExtendedProperties so not Selecting DataColumn.ColumnName and adding as columns.

Code:

DataTable dt1 = new DataTable();
dt1.TableName = "dt1";
dt1.PrimaryKey = new DataColumn[] { dt1.Columns.Add("A") };
dt1.Columns.Add("B");
dt1.Columns.Add("C");
dt1.Columns.Add("D");

DataTable dt2 = new DataTable();
dt2.TableName = "dt2";
dt2.PrimaryKey = new DataColumn[] { dt2.Columns.Add("a") };
dt2.Columns.Add("b");
dt2.Columns.Add("c");
dt2.Columns.Add("d");

var dtResult = dt1.Clone();
dtResult.TableName = "dtResult";

Working fine till here

Now want to merge...

Multiple merge tries type 1

dtResult.Merge(dt2.Clone()); 
dtResult.Merge(dt2.Clone(), false);
dtResult.Merge(dt2.Clone(), true);
dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.Add);
dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.AddWithKey); 
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.Add);
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.AddWithKey);

In all above tries, Columns are merged into dtResult but throws NullReferenceException. StackTrace:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Data
  StackTrace:
   at System.Data.Merger.MergeSchema(DataTable table)
   at System.Data.Merger.MergeTableData(DataTable src)
   at System.Data.Merger.MergeTable(DataTable src)
   at System.Data.DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction)
   at Demo.Program.Test5() in C:\App\Demo\Program.cs:line 54
   at Demo.Program.Main(String[] args) in C:\App\Program.cs:line 36

Multiple merge tries type 2

dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.Error);
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.Error);

In all above tries, Columns are NOT merged into dtResult and System.Data.DataException StackTrace:

System.Data.DataException
  HResult=0x80131920
  Message=Target table  missing definition for column a.
  Source=System.Data
  StackTrace:
   at System.Data.Merger.MergeSchema(DataTable table)
   at System.Data.Merger.MergeTableData(DataTable src)
   at System.Data.Merger.MergeTable(DataTable src)
   at System.Data.DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction)
   at Demo.Program.Test5() in C:\App\Demo\Program.cs:line 54
   at Demo.Program.Main(String[] args) in C:\App\Demo\Program.cs:line 36

Multiple merge tries type 3

dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.Ignore);
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.Ignore);

In all above tries, Columns are NOT merged into dtResult and NO exception

c#
.net
datatable
asked on Stack Overflow Jul 25, 2019 by Prem • edited Jul 25, 2019 by 41686d6564

2 Answers

3

Apparently, the NullReferenceException occurs because the two tables have primary keys. We need to do something to avoid that conflict.

There you go:

var dtResult = dt1.Clone();
dtResult.TableName = "dtResult";

var dt2Cloned = dt2.Clone();
dt2Cloned.PrimaryKey = null;
dtResult.Merge(dt2Cloned);

var primaryKeys = dtResult.PrimaryKey.ToList();
foreach (var col in dt2.PrimaryKey)
{
    primaryKeys.Add(dtResult.Columns[col.ColumnName]);
}
dtResult.PrimaryKey = primaryKeys.ToArray();

This can probably be optimized a little but it gets you the result you need so it's a good start.

answered on Stack Overflow Jul 25, 2019 by 41686d6564
0

In addition to answer by @Ahmed Abdelhameed.

If you are facing problem like Unique column cannot have same value or null is not allowed add below code.

var dt2Cloned = dt2.Clone();
var pks = dt2Cloned.PrimaryKey.ToList();
dt2Cloned.PrimaryKey = null;
pks.ForEach(pk => { pk.Unique = false; pk.AllowDBNull = true; });

You can also do same for PKs of dt1 , if required.

answered on Stack Overflow Jul 26, 2019 by Prem

User contributions licensed under CC BY-SA 3.0