B
Brian P
My ultimate goal is to take data from two different databases, (source
and target) and merge any changes between them into the target database.
To test the theory, I'm working on a small example, but I don't
understand why unchanged rows are marked as modified.
When running the code, the results are:
--------------------------------------------------
ID: 1 Value: Red Status: Modified
ID: 2 Value: Blue Status: Modified
ID: 3 Value: Green Status: Added
--------------------------------------------------
I don't understand why Red is marked as modified, since it is identical
in both the source and target data tables.
Any ideas?
--Brian
Here is my code:
static void MergeTest()
{
DataSet dsSource = new DataSet("Source");
DataTable dtSource = new DataTable("Colors");
dtSource.Columns.Add("ColorID", Type.GetType("System.Int32"));
dtSource.Columns.Add("ColorName", Type.GetType("System.String"));
dtSource.PrimaryKey = new DataColumn[] {dtSource.Columns["ColorID"]};
dtSource.Rows.Add(new object[] {1, "Red"});
dtSource.Rows.Add(new object[] {2, "Blue"});
dtSource.Rows.Add(new object[] {3, "Green"});
dtSource.AcceptChanges();
DataSet dsTarget = new DataSet("Target");
DataTable dtTarget = new DataTable("Colors");
dsTarget.Tables.Add(dtTarget);
dtTarget.Columns.Add("ColorID", Type.GetType("System.Int32"));
dtTarget.Columns.Add("ColorName", Type.GetType("System.String"));
dtTarget.PrimaryKey = new DataColumn[] {dtTarget.Columns["ColorID"]};
dtTarget.Rows.Add(new object[] {1, "Red"});
dtTarget.Rows.Add(new object[] {2, "Black"});
dtTarget.Rows.Add(new object[] {4, "Yellow"});
dtTarget.AcceptChanges();
foreach (DataRow row in dtSource.Rows)
{
dtTarget.LoadDataRow(row.ItemArray, false);
}
Console.WriteLine(dsTarget.HasChanges());
DataTable delta;
delta = dtTarget.GetChanges();
foreach (DataRow row in delta.Rows)
{
Console.WriteLine("ID: {0} Value: {1} Status: {2}",
row["ColorID"].ToString().PadRight(4),
row["ColorName"].ToString().PadRight(10),
row.RowState);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press Enter to Continue...");
Console.ReadLine();
}
and target) and merge any changes between them into the target database.
To test the theory, I'm working on a small example, but I don't
understand why unchanged rows are marked as modified.
When running the code, the results are:
--------------------------------------------------
ID: 1 Value: Red Status: Modified
ID: 2 Value: Blue Status: Modified
ID: 3 Value: Green Status: Added
--------------------------------------------------
I don't understand why Red is marked as modified, since it is identical
in both the source and target data tables.
Any ideas?
--Brian
Here is my code:
static void MergeTest()
{
DataSet dsSource = new DataSet("Source");
DataTable dtSource = new DataTable("Colors");
dtSource.Columns.Add("ColorID", Type.GetType("System.Int32"));
dtSource.Columns.Add("ColorName", Type.GetType("System.String"));
dtSource.PrimaryKey = new DataColumn[] {dtSource.Columns["ColorID"]};
dtSource.Rows.Add(new object[] {1, "Red"});
dtSource.Rows.Add(new object[] {2, "Blue"});
dtSource.Rows.Add(new object[] {3, "Green"});
dtSource.AcceptChanges();
DataSet dsTarget = new DataSet("Target");
DataTable dtTarget = new DataTable("Colors");
dsTarget.Tables.Add(dtTarget);
dtTarget.Columns.Add("ColorID", Type.GetType("System.Int32"));
dtTarget.Columns.Add("ColorName", Type.GetType("System.String"));
dtTarget.PrimaryKey = new DataColumn[] {dtTarget.Columns["ColorID"]};
dtTarget.Rows.Add(new object[] {1, "Red"});
dtTarget.Rows.Add(new object[] {2, "Black"});
dtTarget.Rows.Add(new object[] {4, "Yellow"});
dtTarget.AcceptChanges();
foreach (DataRow row in dtSource.Rows)
{
dtTarget.LoadDataRow(row.ItemArray, false);
}
Console.WriteLine(dsTarget.HasChanges());
DataTable delta;
delta = dtTarget.GetChanges();
foreach (DataRow row in delta.Rows)
{
Console.WriteLine("ID: {0} Value: {1} Status: {2}",
row["ColorID"].ToString().PadRight(4),
row["ColorName"].ToString().PadRight(10),
row.RowState);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press Enter to Continue...");
Console.ReadLine();
}