SqlCommand备份数据库 [英] SqlCommand back up Database

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

问题描述

我有一个c#winforms应用程序(.net 2框架)。
我需要从应用程序备份数据库。
我试图通过异步执行SqlCommand来做到这一点。
该代码无一例外地执行,但我没有在目的地获取.bak文件...

I have an c# winforms application (.net 2 framework). I need to backup data bases from my application. I am trying to do this by executing an SqlCommand asynchronously. The code is executed with no exceptions but I dont get the .bak file in my destination...

这是代码:

#region backup DB using T-SQL command

string connString = "Data Source=" + ConfigurationManager.AppSettings.Get("localhost_SQLEXPRESS") + ";Initial Catalog=" + db + ";UserID=" + ConfigurationManager.AppSettings.Get("user") + ";Password=" + ConfigurationManager.AppSettings.Get("password");
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connString);
builder.AsynchronousProcessing = true;
using (SqlConnection sqlConnection1 = new SqlConnection(builder.ConnectionString))
{
    using (SqlCommand cmd = new SqlCommand("BACKUP DATABASE " + db + " TO DISK=" + location + "\\" + ConfigurationManager.AppSettings.Get("DataBaseBackupsFolderName") + "\\" + db + ".bak'", sqlConnection1))
    {    
        sqlConnection1.Open();    
        IAsyncResult result = cmd.BeginExecuteNonQuery();

        while (!result.IsCompleted)
        {
            Thread.Sleep(100);
        }
    }
}
#endregion


推荐答案

在您的SQL备份行中,您似乎在备份文件路径的开头缺少单引号。

In your SQL backup line you seem to be missing a single quote at the beginning of the path to the backup file.

  using (SqlCommand cmd = new SqlCommand("BACKUP DATABASE " + db + " TO DISK='" + location + "\\" + ConfigurationManager.AppSettings.Get("DataBaseBackupsFolderName") + "\\" +db + ".bak'", sqlConnection1)) 

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

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