[英]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 我有一个Windows mobile 6应用程序,当服务器上有可用的新补丁程序时,它将下载New Cab文件并使用以下代码在设备上进行编程安装
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. 但是,这也删除了现有的数据库[Sql CE]和有价值的数据。
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... 我需要将数据库保留在设备上并卸载应用程序并安装新的cab文件,而无需在安装新的cab时再次安装Fresh DB。
How can i achieve this...help me on this???? 我如何实现这一目标...对此有帮助吗????
Before unstalling, backup the database file (typically .sdf). 取消安装之前,请备份数据库文件(通常是.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: 然后,在重新安装后,删除新的CE数据库并还原您的备份:
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. digginforfire的代码将允许您防止该数据库被删除。
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. 例如,您的应用程序在尝试加载数据库之前,应在“
Application Data
文件夹中检查数据库是否存在。 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: 复制和修改>>在这里<<的Microsoft示例的版本,您将具有以下内容:
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. 这将确保卸载产品后数据库仍然存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.