Insert and update doesn't change database (sdf)

邮差的信 提交于 2020-02-07 07:09:24

问题


I have a problem with saving data to database named Settings.sdf. I have few files:

- Settings.sdf
- SettingsDataSet.xsd
 - SettingsDataSet.Designer.cs
 - SettingsDataSet.xsc
 - SettingsDataSet.xss

If I tried use UPDATE or INSERT INTO queries I would not update my database (Settings.sdf), only change data in cache file (whatever and wherever it is). I'm using correctly all SqlCeCommands. For example:

string conString = "Data Source=Settings.sdf";
con = new SqlCeConnection(conString);
con.Open();
...
using (SqlCeCommand com = new SqlCeCommand("UPDATE Settings SET [Value] = (@loc) WHERE [Key] = 'Location'", con))
{
    com.Parameters.AddWithValue("@loc", device.Location);
    com.ExecuteNonQuery();
}
...
con.Close();

How manage this problem? Why this command doesn't update directly my database?


回答1:


You can adjust your database path, you must set full path:

  1. Adjust with:

    string conString = "Data Source=" + Path.Combine(Your full path,"Settings.sdf");`
    
  2. Add ; at the end of your query

using (SqlCeCommand com = new SqlCeCommand("UPDATE Settings SET [Value] = (@loc) WHERE [Key] = 'Location';", con))
{
...
}



回答2:


the changed maded in the sdf file in debug directory but when you run again the sdf will be created again, remove the create always and test again



来源:https://stackoverflow.com/questions/12072169/insert-and-update-doesnt-change-database-sdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!