简体   繁体   English

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

[英]MS Access DB doesnt save changes after execution (C#)

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. 此时行值为1,当我在之后进行选择查询时,插入的行可用。 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 并以这种方式使用DataSet和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

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 提前致谢

Is the database in your bin directory? 数据库是否在bin目录中? 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. 我已经看到这种情况发生在每次构建时都会用项目目录中的数据库覆盖bin目录中的数据库,因此看起来没有保存。

MS Visual Studio builds the Access DB into the product. MS Visual Studio将Access DB构建到产品中。 --- The product is located in your bin directory. ---产品位于bin目录中。 --- 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". 为了解决这个问题:从解决方案资源管理器中选择MS Access DB,按F4转到它的属性,将“复制到输出目录”字段更改为“如果更新则复制”而不是“始终”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM