简体   繁体   English

在ClickOnce中备份localDB数据库

[英]Backup localDB database in ClickOnce

I've created a WPF 4.5 .NET application it with a database backup feature. 我已经创建了具有数据库备份功能的WPF 4.5 .NET应用程序。 The functions and the backup works fine when debugging but when I publish it in ClickOnce and install it in target machine everything works except the backup won't work because ClickOnce obfuscate the app folder location so it becomes too long for the backup statement to work! 功能和备份在调试时可以正常工作,但是当我在ClickOnce中发布并安装到目标计算机中时,一切正常,但备份无法正常工作,因为ClickOnce混淆了应用程序文件夹的位置,因此备份语句的工作时间太长了! Is there a way to make the backup statement shorter? 有没有一种方法可以使备份语句更短? here's my code and the error I get: code: 这是我的代码和我得到的错误:代码:

SaveFileDialog sfd = new SaveFileDialog();
string stringCon = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\whdb.mdf;Integrated Security=True";
string dbPath = Application.StartupPath + @"\whdb.mdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
    using (SqlConnection conn = new SqlConnection(stringCon))
    {
        string backupStmt = string.Format(@"backup database @whdb to disk='{0}' WITH INIT ", sfd.FileName);
        using (SqlCommand sqlComm = new SqlCommand(backupStmt, conn))
        {
            sqlComm.Parameters.AddWithValue("@whdb", dbPath); 
            conn.Open();
            sqlComm.ExecuteNonQuery();
            conn.Close();
        }
    }
)

************** Exception Text ************** **************例外文字**************

System.Data.SqlClient.SqlException (0x80131904): Invalid database name 'C:\Users\Abubaker\AppData\Local\Apps\2.0\52WR4JTO.12O\D6M4D7OQ.Z3D\sa3a..tion_fef19ab42c2b8f22_0001.0000_9fc10c82bbf23ed2\whdb.mdf' specified for backup or restore operation.
BACKUP DATABASE is terminating abnormally.

The whdb.mdf database file is create by another existing application? whdb.mdf数据库文件是由另一个现有应用程序创建的吗? And your WPF application is only for Backup the existing whdb.mdf database? 您的WPF应用程序仅用于备份现有的whdb.mdf数据库吗?

I also struggled for days for this kind of situation. 对于这种情况,我也挣扎了几天。 I tried |Data Directory| 我试过|数据目录| and Application.StartupPath and some other approaches as you've found as well. 以及Application.StartupPath和您发现的其他一些方法。

However, I finally chose this way and successfully launched(published) my service. 但是,我最终选择了这种方式并成功启动(发布)了我的服务。

According to my experience, |Data Directory| 根据我的经验,|数据目录| indicates different places depending on circumstances, in other word, it's not always same.. And I could read (SQL Select command) database with |Data Directory| 根据情况指示不同的位置,换句话说,并不总是相同。。而且我可以使用| Data Directory |读取(SQL Select命令)数据库。 setting in connectionString but couldn't insert or update the database. 在connectionString中设置,但无法插入或更新数据库。 You can find some people are having this difficulties by searching on interent. 通过搜索interent,您会发现有些人遇到了这种困难。 I think when we set |Data Directory| 我想当我们设置|数据目录| in connectionString, the database plays a role as read-only data file like the nuance of |Data Directory|. 在connectionString中,数据库起着只读数据文件的作用,就像| Data Directory |的细微差别一样。

Instead the |Data Directory|, I chose more obvious path which indicates always same place(directory). 代替了| Data Directory |,我选择了更明显的路径来指示始终相同的位置(目录)。

@"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Environment.GetEnvironmentVariable("APPDATA") + @"\whdb.mdf;Integrated Security=True";

Above Environmental variable indicates always C:\\Users\\Abubaker\\AppData\\Roaming directory. 上面的环境变量表示始终为C:\\ Users \\ Abubaker \\ AppData \\ Roaming目录。

For this scenario, you're needed to create the whdb.mdf database file with above path first. 对于这种情况,首先需要使用上述路径创建whdb.mdf数据库文件。

And further, you may create your own additional directory like mybackup with the connectionString as, 此外,您可以使用connectionString创建您自己的附加目录,例如mybackup,

@"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Environment.GetEnvironmentVariable("APPDATA") + @"\mybackup" + @"\whdb.mdf;Integrated Security=True";

I couldn't find the official document from Microsoft how the |Data Directory| 我找不到Microsoft的官方文档|数据目录| works but failed. 可行,但失败了。 Understanding |Data Directory| 了解|数据目录| would be your key, otherwise, there's alternative way like my case. 将是您的钥匙,否则,会有像我这样的情况。 My case is also WPF, ClickOnce, SQL Database (it was localDB but I changed to normal SQL Database because of remote networking) 我的案例也是WPF,ClickOnce,SQL数据库(它是localDB,但由于远程联网而更改为普通的SQL数据库)

通过在连接字符串中添加Initial catalog=whdb来解决该问题,然后仅用数据库名称“ whdb”替换完整路径Application.StartupPath + @"\\whdb.mdf" whdb.mdf”!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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