Search code examples
c#databasesql-server-ce

Insert and update doesn't change database (sdf)


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?


Solution

  • 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))
    {
    ...
    }