public void Merge_ByDataTable()
{
//create source dataset
var ds = new DataSet();
//create datatable
DataTable dt = DataProvider.CreateParentDataTable();
dt.TableName = "Table1";
//add a copy of the datatable to the dataset
ds.Tables.Add(dt.Copy());
dt.TableName = "Table2";
//add primary key
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
ds.Tables.Add(dt.Copy());
//now the dataset hase two tables
//create target dataset (copy of source dataset)
DataSet dsTarget = ds.Copy();
dt = ds.Tables["Table2"];
//update existing row
dt.Select("ParentId=1")[0][1] = "OldValue1";
//add new row
object[] arrAddedRow = new object[] { 99, "NewValue1", "NewValue2", new DateTime(0), 0.5, true };
dt.Rows.Add(arrAddedRow);
//delete existing rows
foreach (DataRow dr in dt.Select("ParentId=2"))
{
dr.Delete();
}
// Merge - changed values
dsTarget.Merge(dt);
Assert.Equal("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1]);
// Merge - added values
Assert.Equal(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray);
// Merge - deleted row
Assert.Equal(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length);
//when merging a DataTable with TableName=null, GH throw null reference exception.
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Col1");
dt.Rows.Add(new object[] { 1 });
// Merge - add a table with no name
ds.Merge(dt);
Assert.Equal(1, ds.Tables.Count);
// Merge - add a table with no name - check Rows.Count
Assert.Equal(dt.Rows.Count, ds.Tables[0].Rows.Count);
}