MS Access DB 执行后不保存更改(C#)

发布于 2024-10-20 06:59:07 字数 1002 浏览 2 评论 0原文

我有以下使用 Access 数据库的代码,

 OleDbConnection con = new OleDbConnection(myproject.Properties.Settings.Default.myDBConnectionString);
 con.Open();
 OleDbCommand command = new OleDbCommand("INSERT INTO components (name) VALUES (@p_col1)", con);
 command.Parameters.Add("@p_col1", OleDbType.VarChar).Value = "test row";
 int rows = command.ExecuteNonQuery();

此时 rows 值为 1,当我在之后进行选择查询时,插入的行可用。当程序完成时,问题就出现了:在进一步的执行中,该行不再存在。

我尝试过以

OleDbTransaction transaction = con.BeginTransaction();
command.Transaction = transaction;
transaction.Commit();

这种方式处理事务并使用 DataSets 和 ADO,

//... add row to dataset ...
OleDbDataAdapter sda = new OleDbDataAdapter("select * from components", con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(sda);

sda.Update(ds.Components); //tried with ds.Components.AcceptChanges(); before and after this line

但在每种情况下我都有同样的问题,似乎插入查询没有在真正的数据库中完成。你知道为什么会发生这种情况吗???

提前致谢

I have the following code for using an access database

 OleDbConnection con = new OleDbConnection(myproject.Properties.Settings.Default.myDBConnectionString);
 con.Open();
 OleDbCommand command = new OleDbCommand("INSERT INTO components (name) VALUES (@p_col1)", con);
 command.Parameters.Add("@p_col1", OleDbType.VarChar).Value = "test row";
 int rows = command.ExecuteNonQuery();

At this point rows value is 1 and when I make select queries after that the row inserted is available. The problem comes when the program finishes: in further executions that row isn´t there anymore.

I´ve tried with transactions

OleDbTransaction transaction = con.BeginTransaction();
command.Transaction = transaction;
transaction.Commit();

and using DataSets and ADO this way

//... add row to dataset ...
OleDbDataAdapter sda = new OleDbDataAdapter("select * from components", con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(sda);

sda.Update(ds.Components); //tried with ds.Components.AcceptChanges(); before and after this line

but in every case i have the same problem, seems like the insert query is not done in the real database. Do you know why can this be happening???

Thanks in advance

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

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

发布评论

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

评论(3

熊抱啵儿 2024-10-27 06:59:07

数据库在你的bin目录下吗?它也是您项目的一部分吗?我已经看到这种情况发生,当你每次构建它时,它都会用项目目录中的数据库覆盖 bin 目录中的数据库,因此看起来事情没有得到保存。

Is the database in your bin directory? Is it also part of your project? I have seen this happen when every time you build it overwrites the database in your bin directory with the one from the project directory, so it appears things are not getting saved.

不羁少年 2024-10-27 06:59:07

可能有多个数据库,而您没有插入到您认为要插入的数据库中。

There may be more than one database and you are not inserting to the one you think you are inserting to.

雄赳赳气昂昂 2024-10-27 06:59:07

MS Visual Studio 将 Access DB 构建到产品中。
--- 该产品位于您的 bin 目录中。
--- 要通过应用程序查看对数据库的任何和所有更改,请使用此选项
当您最初将数据库添加到项目时,它被设置为始终在构建时更新产品。
这可能是一件好事,但我觉得这很烦人。
为了解决这个问题:
从解决方案资源管理器中选择 MS Access DB,
按 F4 转到它的属性,
将字段“复制到输出目录”更改为“如果较新则复制”而不是“始终”。

MS Visual Studio builds the Access DB into the product.
--- The product is located in your bin directory.
--- To view any and all changes to your DB via application use this one
When you initially add the DB to the project, it is set to always update the product on build.
This can be a good thing, but I find it rather annoying.
In order to fix this:
Select the MS Access DB from your Solution Explorer,
Hit F4 to go to it's properties,
Change the field "Copy to Output Directory" to "Copy if newer" instead of "Always".

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