Question

Install SQL Server CE database in a separate folder, and application in other folder as usual. While uninstalling application from device, database won't delete. When reinstalling application on the same device, check if database exist in it [where we saved on first install], if not exist save in common folder for all smart devices [like \Program Files\], else use existing DB .

How can I do this using C#, Windows Mobile 6?????

Was it helpful?

Solution

After reading your comment, I'd suggest something similar to the following:

  • When your application starts, have it check for the existence of your database at the path you require (or at a path specified in an INI file)

  • If the database is not found, ask the user if the default database should be used (or, just copy it over if they should have no choice).

When the application is removed, this database will remain.

When your application is reinstalled, it can use the database that was left.

Example:

namespace DBTest1 {

  public partial class Form1 : Form {

  const readonly string MYDATABASE = "\\Application Data\\DBTest1\\sqlce.db";

  private void Form1() {
    InitializeComponent();
    CreateIfNotThere(DBTest1.Properties.Resources.sqlcedb, MYDATABASE);
  }

  void CreateIfNotThere(object data, string filename) {
    if (String.IsNullOrEmpty(filename)) {
      filename = string.Format(@"{0}{1}{2}",
      Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
      Path.DirectorySeparatorChar, "sqlCe.db");
    }
    if (data != null) {
      byte[] array = (byte[])data;
      FileInfo file = new FileInfo(filename);
      if (!file.Exists) {
        using (FileStream fs = file.Create()) {
          fs.Write(array, 0, array.Length);
        }
      }
    }
  }
  // other code
}

If you want to get into writing custom CABWIZ steps, there is a nice write up in the How to create the inf file for a smart device cab project from command line? thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top