简体   繁体   中英

While unsitalling the Windows mobile application , how to Keep SQL CE Database with Existing DataOn device?

I have a Windows mobile 6 application.When a new patch is Available on server it will download the New Cab file and install progrmatically on the device with the below Code

Process proc = Process.Start("wceload.exe", "\"" + Path.Combine(applicationPath, updateFileName) + "\"");
  proc.WaitForExit();

But this is deleting The existing Database[Sql CE] and Valuable Datas also.

I need to keep the Database On device and uninstall the application and install the new cab file where no need to install the Fresh DB again along with NEW cab installation...

How can i achieve this...help me on this????

Before unstalling, backup the database file (typically .sdf). After the installation, replace the new database file with your backup.

// backup the CE database
string databasePath = ""; // location of your database
string backupPath = ""; // choose a backup path
try
{
    File.Copy(databasePath, backupPath);
}
catch 
{
    Trace.TraceError("Error creating backup");
}


Process proc = Process.Start("wceload.exe", "\"" + Path.Combine(applicationPath, updateFileName) + "\"");
proc.WaitForExit();

Then, after re-installation, delete the new CE database and restore your backup:

if (File.Exists(databasePath))
{
    try 
    {
         File.Delete(databasePath);
    }
    catch 
    {
         Trace.TraceError("Error deleting new database");
    }
}

string databasePath = ""; // location of your database
string backupPath = ""; // choose a backup path
try
{
    File.Copy(backupPath , databasePath);
}
catch 
{
    Trace.TraceError("Error restoring backup");
}

If you can't delete the new database because it is in use, you'll need to come up with a mechanism to work around that (for example, renaming the new DB, then copying your backup, and then rebooting your application/device)

The code by digginforfire will allow you to prevent this database from being deleted.

Personally, though, I think it is better to write code that ensures this is not necessary. For example, your application should check for the database's existence in the Application Data folder before it attempts to load it. If the database is not there, you simply create it.

Copying and modifying a version of Microsoft's example found >> HERE << , you'd have something like this:

string appPath = string.Format(@"{0}{1}{2}{1}{3}",
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    Path.DirectorySeparatorChar, "MyCompany", "MyProduct"
  );

string dbFile = "database.swf";
string dbFilePath = Path.Combine(appPath, dbFile);
if (!File.Exists(dbFilePath)) {
  string connStr = string.Format("Data Source={0}; Password ={1}", dbFilePath, "1234567");
  using (SqlCeEngine engine = new SqlCeEngine(connStr)) {
    engine.CreateDatabase();
  }
  using (SqlCeConnection conn = new SqlCeConnection(connStr)) {
    try {
      conn.Open();
      using (SqlCeCommand cmd = new SqlCeCommand("CREATE TABLE myTable (col1 int, col2 ntext)", conn)) {
        cmd.ExecuteNonQuery();
      }
    } catch { } finally {
      conn.Close();
    }
  }
}

This will ensure that the database remains after your product is uninstalled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM