使用 TableAdapter 将行插入 Access 2003 DB

发布于 2024-08-15 13:04:27 字数 1081 浏览 2 评论 0原文

我是 TableAdapters 的新手,我不确定发生了什么。本质上,我试图使用生成的数据集将数据从一个数据库导出到具有不同架构的另一个数据库。当我单步执行代码时,就填充目标数据库的行而言,一切似乎都工作正常。但是,当我尝试将一行添加到目标数据库时,该行似乎没有被插入。你们有什么想法吗?我已将添加到项目中的数据库设置为不复制到输出目录...所以我在网上看到的建议似乎不起作用。

        OleDbConnection oleDbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\database.mdb;");

        SomeTableAdapter tableAdapter = new SomeTableAdapter();
        tableAdapter.Connection = oleDbConnection;
        tableAdapter.Connection.Open();

        SomeDataSet.SomeDataTable dataTable = tableAdapter.GetData();
        SomeDataSet.SomeDataRow dataRow = null;

        // Do some checks on the existing rows

        // Creation of new row is necessary
        if (dataRow == null)
            dataRow = dataTable.NewSomeRow();

            // Populate row fields

            dataTable.AddSomeRow(dataRow);
            dataTable.AcceptChanges();
        }
        else
        {
            // Update exiting row
        }

        tableAdapter.Update(dataTable);
        tableAdapter.Connection.Close();

I'm new to using TableAdapters and I am unsure of what is going on. Essentially, I am trying to export data from one database to another database with a different schema using a generated dataset. Everything seems to be working fine, in terms of populating the row of the target database, when I step through the code. However, when I try to have a row added to the target database, the row does not seem to get inserted. Do you guys have any ideas? I have set the database that was added to the project set to not copy to the output directory...so the suggestions I saw on the web didn't seem to work.

        OleDbConnection oleDbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\database.mdb;");

        SomeTableAdapter tableAdapter = new SomeTableAdapter();
        tableAdapter.Connection = oleDbConnection;
        tableAdapter.Connection.Open();

        SomeDataSet.SomeDataTable dataTable = tableAdapter.GetData();
        SomeDataSet.SomeDataRow dataRow = null;

        // Do some checks on the existing rows

        // Creation of new row is necessary
        if (dataRow == null)
            dataRow = dataTable.NewSomeRow();

            // Populate row fields

            dataTable.AddSomeRow(dataRow);
            dataTable.AcceptChanges();
        }
        else
        {
            // Update exiting row
        }

        tableAdapter.Update(dataTable);
        tableAdapter.Connection.Close();

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

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

发布评论

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

评论(1

七度光 2024-08-22 13:04:27

调用 AcceptChanges() 等于搬起石头砸自己的脚。它将对数据集的所有修改标记为“已提交”,因此下一次更新会忽略它们。

通常,您不应直接调用 AcceptChanges,除非在高级场景中。

You are shooting yourself in the foot by calling AcceptChanges(). It marks all the modifications to the dataset as 'committed' so the next Update simply ignores them.

You generally shouldn't ever call AcceptChanges directly, except in advanced scenarios.

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