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

后端 未结 3 522
青春惊慌失措
青春惊慌失措 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 17:57
    SqlCeConnection conn = new SqlCeConnection(@"Data Source = c:\FullPath") 
    

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

    0 讨论(0)
  • 2021-01-14 18:09

    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?

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题