none
Strongly-typed dataset merging behaviour RRS feed

  • Question

  • Hi,

    I'm trying to understand how DataSet.Merge() works and if it could be useful in my project..
    I created a strongly-typed dataset, i.e. MyDataSet, that contains a single DataTable, i.e. MyDTable, with a (string) primary key column and a second string column..

    What I need is to compare/merge a MyDataSet created and filled at runtime by my app with a MyDataSet filled from db, with the aim to synchronize the result back to db (i.e. CRUD operations)..

    I wrote a simple example to check Merge() behaviour, but what I got wasn't what I expected:

    Code Block

    //think of myds1 as the dataset filled from db
    MyDataSet myds1 = new MyDataSet();
    myds1.MyDTable.AddMyDTableRow("1", "first row");
    myds1.MyDTable.AddMyDTableRow("2", "second row");
    myds1.MyDTable.AddMyDTableRow("3", "third row");

    myds1.AcceptChanges();  //change Rows' RowState to "Unchanged"

    //this one is the dataset filled by my app
    MyDataSet myds2 = new MyDataSet();
    myds2.MyDTable.AddMyDTableRow("1", "first row");  //the same as myds1
    myds2.MyDTable.AddMyDTableRow("3", "updated third row");

    myds1.Merge(myds2);  //do the merge
    foreach (MyDataSet.MyDTableRow row in _ds1.MyDTable)
    {
         Console.WriteLine("{0}:{1} - {2}", row.Column1,row.Column2,row.RowState);
    }



    The output I get is:
    Code Block

    1:first row - Modified
    2:second row - Unchanged
    3:updated third row - Modified



    While I was expecting something like:
    Code Block

    1:first row - Unchanged
    2:second row - Deleted
    3:updated third row - Modified



    Is there a way to change Merge() behaviour? What I'm doing wrong?

    Thanks
    S-





    Wednesday, October 24, 2007 8:24 AM

Answers

  • Yep!
    In fact, after many tries, I've been finally defeated and I opted for a custom merge logic i.e. comparing the datasets row by row.. (completely ignoring performance issuses! or not?)

    Thanks,
    S-
    Wednesday, October 24, 2007 5:05 PM

All replies

  • I believe this is what's happening:

    The first row gets row state Modified because thats the row state for that row in myds2 (the new source). If you don't want this behavior you should pass a second parameter (preservChanges) and set it to true (I think this will fix it).

    The second row will only get deleted if it is included in the new source with row state Deleted and preserverChanges set to false (which is default).

    The third row works as you expect.

     

    I believe that a more realistic (depending on scenario) test would be if you created a copy of your first dataset, and then on this dataset made a change to the third row and a delete to the second row. Then if you merge you will get the behavior you expect and now you are far from it because the second one has not the expected row states (for example unchanged for first row).

     

    It can be difficult to use Merge for more advanced synchronization and I believe that it's primarly designed for the scenario where the client makes a GetChanges() to send only changed rows the server and when getting that rows back from the server, easily merge them back to the original dataset. With that said you could laborate with using the overloads of Merge that takes an array of DataRows and use Select() against your new source. This could bring some more flexibility, but we have been forced to write our own Merge strategies for more complex scenarios.

     

    Wednesday, October 24, 2007 3:08 PM
  • Yep!
    In fact, after many tries, I've been finally defeated and I opted for a custom merge logic i.e. comparing the datasets row by row.. (completely ignoring performance issuses! or not?)

    Thanks,
    S-
    Wednesday, October 24, 2007 5:05 PM