我有以下使用 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();
并以这种方式使用数据集和 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
但在每种情况下我都有同样的问题,似乎插入查询没有在真实数据库中完成。你知道为什么会这样吗???
提前致谢
请您参考如下方法:
数据库在你的bin目录下吗?它也是您项目的一部分吗?我已经看到这种情况发生在每次构建时它都会用项目目录中的数据库覆盖 bin 目录中的数据库,所以看起来事情没有得到保存。
