3

I am doing a project on Visual Studio. I am using a local database (empty sql server compact edition). I chose Dataset and created my table (Images). It has a primary autoincrement id column, and an nvarchar ImagePath column. I want to insert data in it and here is my code.

SqlCeConnection con = new SqlCeConnection();
con.ConnectionString = yeniApplicationDatabase.Properties.Settings.Default.DatabaseEdaConnectionString;
con.Open();
using (SqlCeCommand com = new SqlCeCommand("INSERT INTO Images (ImagePath) VALUES ('book')", con))
{
  com.ExecuteNonQuery();
}

I don't know why but this one doesn't give any error, the syntax(SQL) is fine. However, when I check the table data, it is still null. Here is the thing;

In the same run,
I execute that code, then I execute another one which is select * from images...
It shows 'book'. But still, the table data is empty, and when I rerun it without inserting, only selecting from Images, it is gone again. I really don't understand what is going on. Why can't I put anything in my database?

I also added con.Close() but it still doesn't work.

3
  • closing the connection should complete the insert Commented Mar 21, 2011 at 17:23
  • i also did it but still it doesn't work. i dont know why. Commented Mar 21, 2011 at 17:24
  • I have updated the answer. If the query doesn't work on connection clode, check the link that I have mentioned in the answer which deals with commiting insert transactions Commented Mar 21, 2011 at 17:35

3 Answers 3

8
SqlCeConnection con = new SqlCeConnection();
con.ConnectionString =  yeniApplicationDatabase.Properties.Settings.Default.DatabaseEdaConnectionString;
con.Open();
SqlCeCommand com = new SqlCeCommand("INSERT INTO Images (ImagePath) VALUES ('book')", con);

com.ExecuteNonQuery();


com.Dispose();
con.Close();

Closing the connection should complete the insert.

EDIT: update on the solution

Which database file(.sdf) are you viewing to check whether the data has been inserted. check the content of the test table in the .sdf in the bin\Debug folder. I believe that your data is inserted properly in the database file which exist in bin\Debug folder.

Just found a similar question on stack overflow: Why can't I insert a record into my SQL Compact 3.5 database? and I firmly believe that your problem is exactly the same. you are checking the wrong database file.

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

31 Comments

this com is out of the using {..} block, it is con or com?
I tried con.Dispose() and con.Close() as you wrote but still it didn't add it.
I have updated the answer. try putting the code in a try catch block. and see if it gives you an error or gets executed successfully
i wrote try{ SqlCommand ..... } catch(Exception e ) and printed out the e but it didn't give any error.
I accepted the answer and I want to accept it a million times more. Thank you so much!
|
2

I do not use Eclipse, but something you must watch out for in Visual Studio when debugging local databases is the Copy file property for the database file in Solution Explorer.

By default this property is "Copy", which means a copy of the database is put in the bin\debug folder of your project and the connection string is to the copy. If you change the copy, your original file is not changed and the changes are discarded when your debug session exits.

See if your IDE copies the database and inspect the connection string. Also how are you "checking the data table"? Are you using the same connection string? Are you doing this after the debug session ends?

2 Comments

I write an sql statement like (select * from images). this is how I check. Is it wrong? And how can I check whether it copies? Do I need to close and open the connection each time after I execute a query? And another question is that do I have to lose all the data after I close Visual Studio? I wrote Eclipse by mistake, sorry.
select * from images is fine so long as the connection string used for the select is the same as that used for the insert. Is it? Put a breakpoint on the SqlCeCommand object and check.
0
SqlCeConnection conn = new SqlCeConnection(@"Data Source = c:\FullPath") 

How to get full path: in solution explorer Right click on .Sdf then get fullPath.

Comments

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.