DataTable.Merge and DataTable.ImportRow does not change RowState
Asked Answered
N

2

5

I am having issues with ADO.NET 2.0 merging/importing data. I need to update/insert data from one generic table to another table with both tables maintaining an identical schema. The following code works great locally, but does not make changes to the database:

        OleDbDataAdapter localDA = loadLocalData();            
        OleDbDataAdapter hostedDA = loadHostedData();            

        DataSet dsLocal = new DataSet();            
        localDA.Fill(dsLocal);

        DataSet dsChanges = new DataSet();
        hostedDA.Fill(dsChanges);

        dsLocal.Tables[0].Merge(dsChanges.Tables[0],false);

        localDA.Update(dsLocal.Tables[0]);

The same is true with this code snippet:

        OleDbDataAdapter localDA = loadLocalData();
        OleDbDataAdapter hostedDA = loadHostedData();

        DataSet dsLocal = new DataSet();
        localDA.Fill(dsLocal);

        DataSet dsChanges = new DataSet();
        hostedDA.Fill(dsChanges);

        foreach (DataRow changedRow in dsChanges.Tables[0].Rows)
        {
            if (recordExists(dsLocal.Tables[0], changedRow["ID"]))
            {

            }
            else
            {
                dsLocal.Tables[0].ImportRow(changedRow);
            }
        }

        localDA.Update(dsLocal.Tables[0]);

When I looked at the RowState property for changed/appended rows they remain "unchanged". I am wanting to avoid data mapping the columns if possible, which is what I may have to do using the NewRow() method and modifying an existing row.

Nieman answered 22/9, 2009 at 15:55 Comment(1)
I can come up with a solution that modifies the RowState when using ImportRow. The following code snippet will work: dsLocal.Tables[0].ImportRow(changedRow); dsLocal.Tables[0].Rows[dsLocal.Tables[0].Rows.Count - 1].SetAdded(); However, this only works on appending rows and not updating rows. Any ideas on merge would be very helpful!Nieman
S
6

You need to fire off the appropriate function for each DataRow.

  • DataRow.SetAdded()
  • DataRow.SetModified()

This will update the DataRow.RowState value. This is what a DataAdapter goes by to determine which rows need what actions performed on them.

It would be nice if their was something like a binded dataset in .net so that it would manage these mundane details for us.

Scrub answered 27/10, 2009 at 15:32 Comment(2)
note, when I use row.SetAdded(), I received an exception stating: SetAdded and SetModified can only be used when the row status is Unchanged.Ferule
You only want to use one of them at a time.Cubbyhole
U
6

To get the behavior you want, I did the following instead of dsLocal.Tables[0].Merge(dsChanges.Tables[0],false);:

using (DataTableReader changeReader = new DataTableReader(dsChanges.Tables[0]))
    dsLocal.Tables[0].Load(newTableReader, LoadOption.Upsert);

This reads the changes table and "upserts" into the 'local' table, giving you the appropriate change notifications.

Urochrome answered 21/1, 2013 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.