我正在尝试使用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'.
根据我的阅读,这是因为运行代码的用户没有正确的权限。我不知道代码以什么用户身份运行,也不知道如何更改。
请问我是否可以在不更改用户的情况下解决此问题,如果不能,请-如何更改?