在ClickOnce中备份localDB数据库 [英] Backup localDB database in ClickOnce

查看:72
本文介绍了在ClickOnce中备份localDB数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了具有数据库备份功能的WPF 4.5 .NET应用程序。这些功能和备份在调试时可以正常工作,但是当我在ClickOnce中发布并安装到目标计算机中时,一切正常,除了备份无法正常工作,因为ClickOnce混淆了应用程序文件夹的位置,因此备份语句的工作时间太长了!有没有一种方法可以使备份语句更短?这是我的代码和收到的错误:
代码:

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.


推荐答案

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

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"!

这篇关于在ClickOnce中备份localDB数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆