gpt4 book ai didi

c# - 如何使用c#从azure存储容器恢复数据库

转载 作者:行者123 更新时间:2023-12-03 04:17:59 26 4
gpt4 key购买 nike

我们有一个 azure 的存储容器,每天我们都在其中保存数据库备份文件。现在我们正在开发一个使用SMO(C#)的应用程序来从Azure存储容器恢复数据库。从本地设备恢复工作正常,但我没有得到任何有关使用 c# 从 azure 容器恢复的帮助。

当我使用 azure blob 恢复备份时,出现以下错误

Cannot open backup device 'https://myaccount.blob.core.windows.net/backupcontainer/Database_e95aec6ab14a43a3a14a62f82b2c7208_20180718022621-04.bak'. Operating system error 3(The system cannot find the path specified.). RESTORE FILELIST is terminating abnormally

我的代码是

Restore restoreDB = new Restore();
restoreDB.Action = RestoreActionType.Database;
restoreDB.Database = databaseName;
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.Url, CredentialName);
restoreDB.Devices.Add(deviceItem);
restoreDB.ReplaceDatabase = true;

RelocateFile DataFile = new RelocateFile();
string MDF = restoreDB.ReadFileList(smoServer).Rows[0][1].ToString();
DataFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[0][0].ToString();
DataFile.PhysicalFileName = smoServer.Databases[databaseName].FileGroups[0].Files[0].FileName;

RelocateFile LogFile = new RelocateFile();
string LDF = restoreDB.ReadFileList(smoServer).Rows[1][1].ToString();
LogFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[1][0].ToString();
LogFile.PhysicalFileName = smoServer.Databases[databaseName].LogFiles[0].FileName;

restoreDB.RelocateFiles.Add(DataFile);
restoreDB.RelocateFiles.Add(LogFile);
restoreDB.NoRecovery = false;
restoreDB.PercentComplete += new PercentCompleteEventHandler(rstDatabase_PercentComplete);
restoreDB.Complete += new ServerMessageEventHandler(rstDatabase_Complete);
restoreDB.SqlRestore(smoServer);

任何人都可以帮助我如何使用 C# 从 azure 存储容器恢复备份文件,或者我的代码有什么问题?

最佳答案

我发现了问题。 SQL Server 2012 或以前的版本不支持从 block blob 恢复数据库。因此,我将 SQL Server 更新到 SQL Server 2017,并更改了我的代码。您需要在 SQL Server 中创建凭据,从 azure 恢复时将使用该凭据。

                if (!smoServer.Credentials.Contains(CredentialName))
{
Credential cr = new Credential(smoServer, CredentialName);
cr.Create("SHARED ACCESS SIGNATURE", AzureDataModel.SharedAccessKey);
}

Restore restoreDB = new Restore();
restoreDB.Action = RestoreActionType.Database;
restoreDB.Database = databaseName;
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.Url);
restoreDB.Devices.Add(deviceItem);
restoreDB.ReplaceDatabase = true;
if (!smoServer.Databases.Contains(databaseName)) {
Database db = new Database(smoServer, databaseName);
db.Create();
}

RelocateFile DataFile = new RelocateFile();
string MDF = restoreDB.ReadFileList(smoServer).Rows[0][1].ToString();
DataFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[0][0].ToString();
DataFile.PhysicalFileName = smoServer.Databases[databaseName].FileGroups[0].Files[0].FileName;

RelocateFile LogFile = new RelocateFile();
string LDF = restoreDB.ReadFileList(smoServer).Rows[1][1].ToString();
LogFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[1][0].ToString();
LogFile.PhysicalFileName = smoServer.Databases[databaseName].LogFiles[0].FileName;

restoreDB.RelocateFiles.Add(DataFile);
restoreDB.RelocateFiles.Add(LogFile);
restoreDB.NoRecovery = false;
restoreDB.PercentComplete += new PercentCompleteEventHandler(rstDatabase_PercentComplete);
restoreDB.Complete += new ServerMessageEventHandler(rstDatabase_Complete);
restoreDB.SqlRestore(smoServer);

关于c# - 如何使用c#从azure存储容器恢复数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com