以其他用户的身份从备份还原SQL数据库

时间:2019-10-10 14:56:32

标签: c# sql-server database database-restore

我正在尝试使用Microsoft.SqlServer.Management.Smo以编程方式还原SQL数据库备份。

到目前为止,这是我的代码:

        Server corp = new Server(SQLServerName);
        Restore restore = new Restore();
        restore.Database = databaseName;
        restore.Action = RestoreActionType.Database;
        restore.Devices.AddDevice(LocalBackupFilePath, DeviceType.File);
        restore.PercentCompleteNotification = 5;
        restore.ReplaceDatabase = true;
        restore.PercentComplete += DatabaseRestorePercentComplete;
        restore.Complete += DatabaseRestoreComplete;

        DataTable dbFiles = restore.ReadFileList(corp);
        string backupLogicalName_MDF = dbFiles.Rows[0][0].ToString();
        string backupLogicalName_LDF = dbFiles.Rows[1][0].ToString();
        restore.RelocateFiles.Add(new RelocateFile(backupLogicalName_MDF, @"G:\Databases"));
        restore.RelocateFiles.Add(new RelocateFile(backupLogicalName_LDF, @"G:\SqlTranslogs"));

        restore.SqlRestore(corp);

执行此操作时会出现此错误:

Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore failed for Server '<server>'.  ---> Microsoft.SqlServer.Management.Smo.SmoException: System.Data.SqlClient.SqlError: The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'G:\Databases'.

根据我的阅读,这是因为运行代码的用户没有正确的权限。我不知道代码以什么用户身份运行,也不知道如何更改。

请问我是否可以在不更改用户的情况下解决此问题,如果不能,请-如何更改?

0 个答案:

没有答案