开发者

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;

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

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

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

        OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
        da.InsertCommand = cb.GetInsertCommand();
        da.DeleteCommand = cb.GetDeleteCommand();
        da.UpdateCommand = cb.GetUpdateCommand();

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

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

        sr.AcceptChanges();

        da.Update(table as DataTable);

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

        table.AcceptChanges();

        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, 开发者_如何学JAVAthat 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜