简体   繁体   中英

OledbDataAdapter doesn't update table

I have a problem updating a data table. What I am doing is this:

1) Load data from database (let's call it 'old database'

2) Load data from other database (let's call it 'new database')

3) Clear out all data from the new database

4) Merge all the old data (from old database) into new database

Here is how I have attempted to accomplish this:

        string oldDatabase = this.txtOldReport.Text;
        string newDatabase = this.txtNewReport.Text;
        string backupFolder = @"C:\Planit\Report Updater\Backups";

        if ( !Directory.Exists ( backupFolder ) ) {
            Directory.CreateDirectory ( backupFolder );
        }

        string fullPath = Path.Combine ( backupFolder, string.Format ( @"Update Backup_{0}_{1}_{2}_{3}_{4}_{5}",
                                                                       DateTime.Now.Year,
                                                                       DateTime.Now.Month,
                                                                       DateTime.Now.Day,
                                                                       DateTime.Now.Hour,
                                                                       DateTime.Now.Minute,
                                                                       DateTime.Now.Second) );

        Directory.CreateDirectory ( fullPath );

        File.Copy ( oldDatabase, Path.Combine ( fullPath, @"Old Report.mdb" ) );
        File.Copy ( newDatabase, Path.Combine ( fullPath, @"New Report.mdb" ) );

        OleDbConnection oldConnection = new OleDbConnection ( string.Format ( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", oldDatabase ) );
        OleDbConnection newConnection = new OleDbConnection ( string.Format ( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", newDatabase ) );
        OleDbCommand oldCommand = oldConnection.CreateCommand ( );
        OleDbCommand newSelectCommand = newConnection.CreateCommand ( );

        oldCommand.CommandText = @"SELECT * FROM Reports";
        newSelectCommand.CommandText = @"SELECT * FROM Reports";

        oldConnection.Open ( );
        newConnection.Open ( );

        DataSet oldDataset = new DataSet ( );
        DataSet newDataset = new DataSet ( );

        OleDbDataAdapter oldAdapter = new OleDbDataAdapter ( oldCommand );
        OleDbDataAdapter newAdapter = new OleDbDataAdapter ( newSelectCommand );

        OleDbCommandBuilder builder = new OleDbCommandBuilder ( newAdapter );

        builder.QuotePrefix = "[";
        builder.QuoteSuffix = "]";

        newAdapter.UpdateCommand = builder.GetUpdateCommand ( true );
        newAdapter.InsertCommand = builder.GetInsertCommand ( true );
        newAdapter.DeleteCommand = builder.GetDeleteCommand ( true );

        oldAdapter.Fill ( oldDataset );
        newAdapter.Fill ( newDataset );

        newDataset.Tables [ 0 ].Rows.Clear ( );

        // removed for a response below
        //foreach ( DataRow dr in newDataset.Tables [ 0 ].Rows ) {
        //    dr.Delete ( );
        //}

        foreach ( DataRow dr in oldDataset.Tables [ 0 ].Rows ) {
            dr.SetAdded ( );
            newDataset.Tables [ 0 ].ImportRow ( dr );
        }

        // removed for the response below
        //newDataset.AcceptChanges ( );

        newAdapter.Update ( newDataset.Tables [ 0 ] );

        oldConnection.Close ( );
        newConnection.Close ( );

    }   

Can anyone see the problem that I have? When I call 'newAdapter.Update' nothing happens.

Thanks for any help.

MSDN says

When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed

So, this line

  newDataset.AcceptChanges ( );

removes the Added RowState from your imported table and when you call Update there is no row to add in the database because every row in the newDataset table has its state equal to Unchanged

So you just need to remove it and call again your newAdapter.Update

As a side note, I don't think you need to loop over the newDataset.Tables[0] to delete every row because you have called newDataset.Tables[0].Rows.Clear() immediately before

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM