1

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

blitz32
  • 13
  • 1
  • 3

3 Answers3

4

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.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • I was overwritting the database in bin directory with the one in the project each time i build. It´s not a stylish solution, but changing the DataSource property of the connectionString so it points to the database in the project directory it works for now. – blitz32 Mar 06 '11 at 15:54
0

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

JGM
  • 1
0

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

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69