Search code examples
c#insertoledb

INSERT query OleDB C#


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? :)


Solution

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