3

I know it was 1000000000 times already, but none solution helped to me. I want to insert data in C# using OleDB. I tried mln solutions but here is the easiest one which should work but it doesn't:

 SQLCONNECTION = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\dbScenariusz.mdb";

 using (OleDbConnection connection = new OleDbConnection(SQLCONNECTION))
            {
                string sql = "INSERT INTO Table (content) VALUES('lala')";
                connection.Open();
                OleDbCommand command = new OleDbCommand(sql, connection);
                command.ExecuteNonQuery();
                connection.Close();
            }

SQLCONNECTION is ok. It works fine for the SELECT query.

string sql - I tried this query in Access and it works fine.

I get no error. It just didn't insert anything to the database. When I run the query in Access (the same database) the row is inserted.

The strange thing is that command.ExecuteNonQuery(); returns 1! That means that 1 row was affected!

I really have no idea where the problem is, so I really appreciate any help. Sorry for my english.

UPDATE: Another strange thing. I change query to update and it works fine! really wtf? :)

3
  • just a guess, as your code seems fine. are you running it on a 64 bit system? Commented Feb 21, 2011 at 13:54
  • Are u sure You're working on the good file ? Commented Feb 21, 2011 at 14:11
  • Well Zitun, 1) I insert the row through Microsoft Access (using the same query) 2) I open the table in Visual Studio and I see the row inserted through Access! But the same thing doesn't work in Visual. I really have no idea what's wrong. Commented Feb 21, 2011 at 14:40

3 Answers 3

3

You are connecting to the DataDirectory. Is the .mbd file being copied after the build? In other words are you re-deploying the database with each build and thereby losing the inserts?

A quick test could be using the same code and same connection to do a select after the insert.

Sign up to request clarification or add additional context in comments.

3 Comments

I hoped this is the problem but no. 1) I run insert query and get return value=1 (1 row affected) 2) I run select query (from the same table) with success 3) I check the table and nothing was inserted 4) Unbelievable!
You were right Erno! Here is the problem: I've got the database in the main directory, the .mdb file is copied to bin/Debug/ and all changes are made on this file. So when I run the application again all changes in database are gone. How should I change it?
Simply by not redeploying the database every single time and placing the database in a directory that doesn't get updated.
2

In the solution Explorer, check app.config. Double click the app.config, then re-enter the connectionString. Give the path of your database. For example, in my project the default location of connectionString is:

connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\database.accdb;Persist Security Info=True"

Suppose the database is stored in this location:

C:\Documents and Settings\Amuk\My Documents\Visual Studio 2010\Projects\insertion\insertion\App_Data

therefore, replace the connectionString with

connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Amuk\My Documents\Visual Studio 2010\Projects\insertion\insertion\App_Data\database.accdb;Persist Security Info=True"

your problem will be solved, i hope this will definitely help you, i was also getting the same problem, and by replacing the connectionString with original path, the database is storing all records.

Comments

0

You can try to use transaction to commit your changes.

command.Transaction = connection.BeginTransaction(); //after connection.open()

Then add

command.Transaction.Commit(); //Before connection.close()

3 Comments

I did exactly like you suggested but nothing has changed.
@arczi - Have you checked properly? There are no errors at all? Apart from this, I dont think there is any issue with your code.
No errors and no warnings. 1) I insert the row through Microsoft Access (using the same query) 2) I open the table in Visual Studio and I see the row inserted through Access! I really have no idea what's wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.