Why can't I insert data into local database (SQL Compact Edition) with C#?

后端 未结 3 541
青春惊慌失措
青春惊慌失措 2021-01-14 17:23

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 autoin

3条回答
  •  [愿得一人]
    2021-01-14 18:16

    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.

提交回复
热议问题