简体   繁体   中英

Whats missing using OleDbDataAdapter.Update to update Access.mdb file?

With the following code below I have managed to open an Access.mpd database and read rows from the table Saved .

However when I try to change data or add new rows I works fine as long as the program is running but nothing seem to be saved to the access.mdb file.

Update: OleDbCommand apparently cannot simply be modified inside the DataAdapter.

Update: AcceptChanges was by me mistakenly used. If used it tells the affected rows to not be updated.

With these updates the code now works. Still I'm looking for understanding of the issue so explanations why will be appreciated. Also If the fixed code is the way to go.

        string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\...\Access.mdb;Persist Security Info=True";
        OleDbConnection conn = new OleDbConnection(connection);
        OleDbDataAdapter da = new OleDbDataAdapter();

        OleDbCommand cmd;

        cmd = new OleDbCommand();
        cmd.CommandText = "Saved";
        cmd.CommandType = CommandType.TableDirect;
        cmd.Connection = conn;
        da.SelectCommand = cmd;

  

        PbDataSet ds = new PbDataSet();
        da.Fill(ds, "Saved");
        PbDataSet.SavedDataTable table = ds.Tables["Saved"] as PbDataSet.SavedDataTable;

        PbDataSet.SavedRow sr = table.Rows[0] as PbDataSet.SavedRow;
        sr.berAktiv = true;   //Changeing data here

Here I try to change the data, which works. However it is not saved to file. this now works!

  PbDataSet.SavedRow sr = table.Rows[0] as PbDataSet.SavedRow; sr.berAktiv = true; //Changeing data here 

        da.Update(table as DataTable);

        sr = table.NewSavedRow();
        sr.rtAktiv = true;
        table.AddSavedRow(sr);

  da.Update(table as DataTable); sr = table.NewSavedRow(); sr.rtAktiv = true; table.AddSavedRow(sr); 

        da.Update(table as DataTable);

  da.Update(table as DataTable); 

No errors are given anywhere.

How can I fix this, so that the data is saved on the file?

How can I verify, in the running program, that it has really been saved, other than reopen the file?

What I can't see is that you're generating the update statements (or let generate them) anywhere...

//Select data
DataSet dataSet = new DataSet();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, db);
dataAdapter.FillSchema(dataSet, SchemaType.Source);
dataAdapter.Fill(dataSet);

//Make changes to the data in the data set...    

//Write changes to the mdb
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter);
dataAdapter.Update(dataSet);

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