Search code examples
c#ms-accessado.netinsertoledb

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. 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


Solution

  • 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.