Merging does not work as documented

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have been reading Core Reference ADO.NET by David Sceppa, a book by MSPress, and had found what I thought was the solution to a problem I have. I am referring to figure 11-8 where two DataTables share the same primary key (say an identity) but have different columns. According to the book, merging these two would result in a change in the number of columns of the receiving DataTable (now the union of each table's columns) -- this much works -- but ALSO, there would be a true merging (not a simple AddRow) according to which rows from the different tables would be matched into a SINGLE row in the destination, with the values of the columns set from the corresponding table

And yet, I cann't get it to work

A brief snippet may show my error

DataSet ds = new DataSet("Mine")
foreach( DataTable table in tables

table.TableName = "PERSONS"
ds.Merge( table, true, MissingSchemaAction.Add)


here tables contains the two tables that have same primary key but different columns

Please help
Juan Den
 
Juan,

Merge relies on the DataTables' PrimaryKey in order to match up rows.
Without this information, the Merge method simply adds rows. Have you set
the PrimaryKey on each of your DataTables? I've enclosed some code I used
to demonstrate the behavior at the end of this post.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2004 Microsoft Corporation. All rights reserved.



DataSet dsA, dsB;
DataTable tblA, tblB;

bool blnCreatePKs = true;

dsA = new DataSet();
tblA = dsA.Tables.Add("Table");
tblA.Columns.Add("ID", typeof(int));
tblA.Columns.Add("ColumnA", typeof(string));

dsB = new DataSet();
tblB = dsB.Tables.Add("Table");
tblB.Columns.Add("ID", typeof(int));
tblB.Columns.Add("ColumnB", typeof(string));

DataRow rowA = tblA.NewRow();
rowA["ID"] = 42;
rowA["ColumnA"] = "From DataSet A";
tblA.Rows.Add(rowA);

DataRow rowB = tblB.NewRow();
rowB["ID"] = 42;
rowB["ColumnB"] = "From DataSet B";
tblB.Rows.Add(rowB);

if (blnCreatePKs)
{
tblA.PrimaryKey = new DataColumn[] {tblA.Columns["ID"]};
tblB.PrimaryKey = new DataColumn[] {tblB.Columns["ID"]};
}

dsA.Merge(dsB);
DisplayHeaders();
foreach (DataRow row in tblA.Rows)
DisplayRow(row);


public void DisplayRow(DataRow row)
{
System.Text.StringBuilder sbOut = new System.Text.StringBuilder();

sbOut.AppendFormat("{0}", row["ID"]);

if (row.IsNull("ColumnA"))
sbOut.AppendFormat("\t{0,-15}", "(null)");
else
sbOut.AppendFormat("\t{0,-15}", row["ColumnA"]);

if (row.IsNull("ColumnB"))
sbOut.AppendFormat("\t{0,-15}", "(null)");
else
sbOut.AppendFormat("\t{0,-15}", row["ColumnB"]);

Console.WriteLine(sbOut.ToString());
}


public void DisplayHeaders()
{
Console.WriteLine("{0}\t{1}\t\t{2}", "ID", "ColumnA", "ColumnB");
Console.WriteLine("{0}\t{1}\t\t{2}", "=======", "=======", "=======");
}
 
Back
Top