使用SMO类将SQL备份还原到其他SQL Server实例时出错

时间:2018-01-03 16:14:23

标签: c# sql sql-server smo

我正在尝试在C#控制台应用程序中使用SMO对象来备份“源”服务器实例上的“源”SQL数据库,然后在不同的“目标”SQL服务器实例上以不同的“目标”名称还原它。备份工作正常,但恢复代码会抛出此异常:

  

内部例外1:   ExecutionFailureException:执行Transact-SQL时发生异常   声明或批次。

     

内部异常2:SqlException:无法打开备份设备   'D:\ Microsoft SQL   服务器\ MSSQL11.MSSQLSERVER \ MSSQL \备份\ mybackupRestore.bak”。   操作系统错误2(系统找不到指定的文件。)。   RESTORE DATABASE异常终止。

但是,如果我使'source'服务器和'target'服务器相同,那么一切都能正常运行。

请参阅下面的CopyDatabase课程和我正在使用的主叫代码。

有没有人有任何想法可能是什么问题?

有关信息,我正在使用SQL Server 2012的实例。

   public static class CopyDatabase
{

    private static void ChangeFile(RelocateFile file, string oldDBName, string newDBName)
    {
        file.PhysicalFileName = file.PhysicalFileName.Replace(oldDBName, newDBName);
    }

    public static string Backup(string sourceServerName,
                            string sourceDatabaseName,
                            string deviceName)
    {
        var sourceServer = new Server(sourceServerName);

        string backupFileFullPath = Path.Combine(sourceServer.BackupDirectory, deviceName);

        Backup backup = new Backup
        {
            Action = BackupActionType.Database,
            Database = sourceDatabaseName,
            Incremental = false,
            CopyOnly = true,
            ExpirationDate = DateTime.Now.AddMinutes(5)
        };

        backup.Devices.AddDevice(deviceName, DeviceType.File);

        backup.SqlBackup(sourceServer);

        return backupFileFullPath;
    }

    public static void Restore(string sourceServerName,
                               string sourceDatabaseName,
                                string targetServerName,
                                string targetDatabaseName,
                                string deviceName)
    {
        var sourceServer = new Server(sourceServerName);
        var targetServer = new Server(targetServerName);

        Restore restore = new Restore();

        restore.Devices.AddDevice(deviceName, DeviceType.File);

        RelocateFile dataFile = new RelocateFile
        {
            LogicalFileName = restore.ReadFileList(sourceServer).Rows[0][0].ToString(),
            PhysicalFileName = sourceServer.Databases[sourceDatabaseName].FileGroups[0].Files[0].FileName
        };

        ChangeFile(dataFile, sourceDatabaseName, targetDatabaseName);

        RelocateFile logFile = new RelocateFile
        {
            LogicalFileName = restore.ReadFileList(sourceServer).Rows[1][0].ToString(),
            PhysicalFileName = sourceServer.Databases[sourceDatabaseName].LogFiles[0].FileName
        };

        ChangeFile(logFile, sourceDatabaseName, targetDatabaseName);

        restore.RelocateFiles.Add(dataFile);
        restore.RelocateFiles.Add(logFile);

        restore.Database = targetDatabaseName;
        restore.NoRecovery = false;
        restore.ReplaceDatabase = true;

        restore.SqlRestore(targetServer);
    }

这是调用代码:

   class Program
{
    static void Main(string[] args)
    {
        string sourceServerName = "<SOURCE SERVER NAME HERE>";
        string sourceDatabaseName = "<SOURCE DB NAME HERE>";
        string targetServerName = "<TARGET SERVER NAME HERE>";
        string targetDatabaseName = "<TARGET DB NAME HERE>";
        string deviceName = "mybackup.bak";

        var backupFileFullPath = CopyDatabase.Backup(sourceServerName, sourceDatabaseName, deviceName);
        CopyDatabase.Restore(sourceServerName, sourceDatabaseName, targetServerName,targetDatabaseName, backupFileFullPath);

        Console.WriteLine("Done!");

    }
}

0 个答案:

没有答案