备份远程SQL Server数据库

时间:2017-03-22 08:10:24

标签: c# sql-server powershell

我想将远程SQL Server数据库备份到本地计算机上。

到目前为止,我已尝试过:

using (SqlConnection defaultSqlConnection = new SqlConnection(Constants.Database_Constants.GetConnectionStringFromProfile(Constants.Profiles.Staging)))
{
    string pathDatabase = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\backup_production_database"+DateTime.UtcNow.ToString("d_MMM_yyyy_HH_mm_ss", CultureInfo.InvariantCulture)+".bak";
    WriteLine("The backup will be stored in " + pathDatabase);

    string backupDb = "BACKUP DATABASE DB_Staging TO DISK = '" + pathDatabase+ "' WITH INIT, COMPRESSION";

    File.Create(pathDatabase);

    using (SqlCommand backupCommand = new SqlCommand(backupDb, defaultSqlConnection))
    {
        defaultSqlConnection.Open();
        backupCommand.ExecuteNonQuery();
    }
}

但是我收到了这个错误:

  

无法打开备份设备' XXXX \ bin \ Debug \ backup_production_database22_Mar_2017_08_04_42.bak'。   操作系统错误3(系统找不到指定的路径。)。   BACKUP DATABASE异常终止。

我知道我可以使用SQL Server Management Studio生成脚本,但它对我没有帮助。我想自动在PowerShell脚本中备份我的数据库,而不必手动进入SQL Server Management Studio

1 个答案:

答案 0 :(得分:0)

您无法创建从远程服务器到本地磁盘的直接备份。

请查看link

相关问题