使用 OleDbDataAdapter.Update 更新 Access.mdb 文件缺少什么?

发布于 2024-08-11 19:01:22 字数 2637 浏览 4 评论 0原文

通过下面的代码,我成功打开了 Access.mpd 数据库并从表 Saved 中读取行。

但是,当我尝试更改数据或添加新行时,只要程序正在运行,我就可以正常工作,但似乎没有任何内容保存到 access.mdb 文件中。

更新: OleDbCommand 显然不能简单地在 DataAdapter 内部进行修改。

更新: AcceptChanges 是我错误使用的。如果使用,它会告诉受影响的行不要更新。

通过这些更新,代码现在可以工作了。我仍然在寻求对这个问题的理解,因此我们将不胜感激解释原因。另外,如果固定代码是可行的方法。

        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;

这里我尝试更改数据,效果很好。但是它没有保存到文件中。现在可以了!

        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);

任何地方都没有给出错误。

我该如何解决这个问题,以便将数据保存在文件中?

除了重新打开文件之外,如何在正在运行的程序中验证它是否确实已保存?

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, that it has really been saved, other than reopen the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2024-08-18 19:01:22

我看不到的是您在任何地方生成更新语句(或让生成它们)......

//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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文