本文介绍了WPF - 如何备份/恢复的LocalDB编程 - 的ClickOnce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用EF和作为的LocalDB它的数据库,通过公布的ClickOnce应用程序。
这是我第一次使用的LocalDB,我不知道我怎样才能增加一个功能,我的应用程序备份/编程恢复数据库。



我的应用通过路径安装ClickOnce的:

  C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0 \NOL11TLW.9XG\CZM702AQ.LPP\basu..tion_939730333fb6fcc8_0001.0002_fd707bbb3c97f8d3 

和这是该数据库文件安装位置:

  C:\Users\Mahdi Rashidi\AppData\Local\Apps\\ \\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu ... exe_939730333fb6fcc8_0001.0002_none_8c555c3966727e7f 




  1. 我应该如何备份/恢复数据库?

  2. 如何保持数据库的安全距离的ClickOnce进一步更新?



非常感谢:)


解决方案

这是我做的备份和恢复我的LocalDB

 使用(TVend2014Entities dbEntities =新TVend2014Entities(公共无效BackupDatabase(字符串文件路径)
{
BaseData.ConnectionString))
{
串backupQuery = @BACKUP DATABASE,{0},TO DISK = N'{1}';
backupQuery =的String.Format(backupQuery,全databsase文件路径,如C:\tempDb.mdf,文件路径);
dbEntities.Database.SqlQuery&所述;对象方式>(backupQuery).ToList()FirstOrDefault();
}
}

使用公共无效RestoreDatabase(字符串文件路径)
{
(TVend2014Entities dbEntities =新TVend2014Entities(BaseData.ConnectionString))
{
串restoreQuery = @使用[大师];
ALTER DATABASE{0}SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE{0}从软盘={1}替换;
ALTER DATABASE,{0}SET MULTI_USER;;
restoreQuery =的String.Format(restoreQuery,全数据库文件路径,文件路径);
无功名单= dbEntities.Database.SqlQuery<对象>(restoreQuery).ToList();
VAR resut = list.FirstOrDefault();
}
}



希望这是你想要的。


I have an application which uses EF and LocalDB as it's database, published by ClickOnce.it's my first time using LocalDB and I don't know how can i add a feature to my application to Backup/Restore The Database Programmatically.

My App Path Installed by ClickOnce :

C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu..tion_939730333fb6fcc8_0001.0002_fd707bbb3c97f8d3

and This is the location which Database files Installed :

C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu...exe_939730333fb6fcc8_0001.0002_none_8c555c3966727e7f
  1. How Should I Backup/Restore the Database?
  2. How Can I Keep Database Safe from ClickOnce further Updates?

Thanks alot :)

解决方案

This is what I did for backup and restore of my localDb

public void BackupDatabase(string filePath)
    {
        using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString))
        {
            string backupQuery = @"BACKUP DATABASE ""{0}"" TO DISK = N'{1}'";
            backupQuery = string.Format(backupQuery, "full databsase file path like C:\tempDb.mdf", filePath);
            dbEntities.Database.SqlQuery<object>(backupQuery).ToList().FirstOrDefault();
        }
    }

    public void RestoreDatabase(string filePath)
    {
        using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString))
        {
            string restoreQuery = @"USE [Master]; 
                                                ALTER DATABASE ""{0}"" SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
                                                RESTORE DATABASE ""{0}"" FROM DISK='{1}' WITH REPLACE;
                                                ALTER DATABASE ""{0}"" SET MULTI_USER;";
            restoreQuery = string.Format(restoreQuery, "full db file path", filePath);
            var list = dbEntities.Database.SqlQuery<object>(restoreQuery).ToList();
            var resut = list.FirstOrDefault();
        }
    }

Hope this is what you want.

这篇关于WPF - 如何备份/恢复的LocalDB编程 - 的ClickOnce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 11:47