Backup localDB database in ClickOnce

久未见 提交于 2019-12-13 07:19:37

问题


I've created a WPF 4.5 .NET application it with a database backup feature. 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! 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.

回答1:


The whdb.mdf database file is create by another existing application? And your WPF application is only for Backup the existing whdb.mdf database?

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.

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| setting in connectionString but couldn't insert or update the database. You can find some people are having this difficulties by searching on 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|.

Instead the |Data Directory|, I chose more obvious path which indicates always same place(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.

For this scenario, you're needed to create the whdb.mdf database file with above path first.

And further, you may create your own additional directory like mybackup with the connectionString as,

@"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| 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)




回答2:


Solved the problem by adding Initial catalog=whdb in the connection string and then replace the full path Application.StartupPath + @"\whdb.mdf" with just the database name "whdb"!



来源:https://stackoverflow.com/questions/42322388/backup-localdb-database-in-clickonce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!