Copy rows using tableadapter doesn't commit changes to disk

74 views Asked by At

I'm trying to copy missing records from one database to another. The two databases are old Access mdbs. I'm using C#, dataset, table adapters, etc. The destination table object ends up with the correct number of rows but the underlying database is not updated. What am I missing? And am I doing roughly the right thing or is there a better, simpler way. The tables in question do not have indexes so I detect whether the record exists by building a Hashset of what should be the key field in the destination table.

The code is below. The data sources and so on were created using the wizard in VS 2019.

I've scoured the web but found only lots of examples of how to copy data table objects, nothing about committing the changes to the underlying database.


namespace ImportTstatDB
{
    class Program
    {
        static void Main(string[] args)
        {
            var srcDataSet = new _TSTAT_SPECDataSetSrc();
            var dstDataSet = new _TSTAT_SPECDataSetDst();
            ImportMissingSpecificationMain(srcDataSet, dstDataSet);
            //ImportMissingResultSets(srcDataSet, dstDataSet);
            dstDataSet.AcceptChanges();
            srcDataSet.RejectChanges();
        }


        private static void ImportMissingSpecificationMain(_TSTAT_SPECDataSetSrc srcDataSet, _TSTAT_SPECDataSetDst dstDataSet)
        {
            var srcTableAdapter = new _TSTAT_SPECDataSetSrcTableAdapters.SpecificationMainTableAdapter();
            srcTableAdapter.Fill(srcDataSet.SpecificationMain);
            var srcTable = srcTableAdapter.GetData();

            var dstTableAdapter = new _TSTAT_SPECDataSetDstTableAdapters.SpecificationMainTableAdapter();
            dstTableAdapter.Fill(dstDataSet.SpecificationMain);
            var dstTable = dstTableAdapter.GetData();

            var transformerIDs = new HashSet<string>();
            foreach (var dstRow in dstTable)
            {
                var transformerID = dstRow.TransformerID;
                transformerIDs.Add(transformerID);
            }
            foreach (var srcRow in srcTable)
            {
                var transformerID = srcRow.TransformerID;
                if (!transformerIDs.Contains(transformerID))
                {
                    // Doesn't exist so copy
                    srcRow.SetAdded();
                    dstTable.ImportRow(srcRow);
                }
            }
            dstTable.AcceptChanges();
            dstTableAdapter.Update(dstTable);

        }
1

There are 1 answers

1
Gustav On

This line I meet in my in-line comments:

// Do NOT call AcceptChanges here as this would clear property RowState.

Thus, try commenting out this line:

// dstTable.AcceptChanges();
dstTableAdapter.Update(dstTable);