将 SQL 服务器备份还原到新数据库时出错
Error restoring SQL Server backup to a new database
我创建了名为 mydb
的 SQL 服务器数据库的备份。我需要使用 C# 代码以编程方式恢复它。
还原必须创建一个名为 mydbnew
的新数据库。我正在使用 Microsoft.SqlServer.Management.Smo
库。
代码是这样的:
public void RestoreDatabase()
{
string databaseName = "mydbnew";
string userName = "user";
string password = "password";
string serverName = "(local)\SQLEXPRESS";
string sourcePath = @"c:\temp\";
string fileName = $"mydbbackup.bak";
ServerConnection connection = new(serverName, userName, password);
Server sqlServer = new Server(connection);
BackupDeviceItem deviceItem = new(sourcePath + fileName, DeviceType.File);
Restore restore = new Restore();
restore.Database = databaseName;
restore.ReplaceDatabase = true;
restore.NoRecovery = false;
restore.Devices.Add(deviceItem);
RelocateFile dataFile = new RelocateFile();
dataFile.LogicalFileName = databaseName + "_data";
dataFile.PhysicalFileName = databaseName + ".mdf";
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = databaseName + "_log";
logFile.PhysicalFileName = databaseName + ".ldf";
restore.RelocateFiles.Add(dataFile);
restore.RelocateFiles.Add(logFile);
restore.SqlRestore(sqlServer);
restore.Devices.Remove(deviceItem);
}
我在 restore.SqlRestore(sqlServer)
处遇到错误:
Logical file 'mydbnew_log' is not part of database 'mydbnew'. Use RESTORE FILELISTONLY to list the logical file names.
RESTORE DATABASE is terminating abnormally.
我的代码有什么问题?
已解决。
我用这个版本更改了重定位部分:
RelocateFile dataFile = new RelocateFile();
dataFile.LogicalFileName = "mydb";
dataFile.PhysicalFileName = Path.Combine(@"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA", "mydbnew.mdf");
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = "mydb_log";
logFile.PhysicalFileName = Path.Combine(@"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA", "mydbnew.ldf");
当然,我必须努力动态获取路径和名称,但它确实有效。
看起来是 2 个问题,第一个:
RelocateFile logFile = new RelocateFile();
dataFile.LogicalFileName = databaseName + "_log";
dataFile.PhysicalFileName = databaseName + ".ldf";
您正在创建名称为 logFile
的对象,但在下一条语句中为旧变量设置值。
我想你希望它是:
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = databaseName + "_log";
logFile.PhysicalFileName = databaseName + ".ldf";
下一期,LogicalFileName
是原始数据库中的实际逻辑名。但是,PhysicalFileName
是 new 名称。
因此,例如,如果您的数据库是这样的,
,
那么代码是这样的:
RelocateFile dataFile = new RelocateFile();
dataFile.LogicalFileName = "Mine";
dataFile.PhysicalFileName = sourcePath + databaseName + ".mdf";
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = "Mine_log";
logFile.PhysicalFileName = sourcePath + databaseName + ".ldf";
RelocateFile
使用旧的逻辑名称(原始数据库),但在 PhysicalFileName
中提到的新位置创建文件
以上代码使用新名称创建文件。
祝你好运。
我创建了名为 mydb
的 SQL 服务器数据库的备份。我需要使用 C# 代码以编程方式恢复它。
还原必须创建一个名为 mydbnew
的新数据库。我正在使用 Microsoft.SqlServer.Management.Smo
库。
代码是这样的:
public void RestoreDatabase()
{
string databaseName = "mydbnew";
string userName = "user";
string password = "password";
string serverName = "(local)\SQLEXPRESS";
string sourcePath = @"c:\temp\";
string fileName = $"mydbbackup.bak";
ServerConnection connection = new(serverName, userName, password);
Server sqlServer = new Server(connection);
BackupDeviceItem deviceItem = new(sourcePath + fileName, DeviceType.File);
Restore restore = new Restore();
restore.Database = databaseName;
restore.ReplaceDatabase = true;
restore.NoRecovery = false;
restore.Devices.Add(deviceItem);
RelocateFile dataFile = new RelocateFile();
dataFile.LogicalFileName = databaseName + "_data";
dataFile.PhysicalFileName = databaseName + ".mdf";
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = databaseName + "_log";
logFile.PhysicalFileName = databaseName + ".ldf";
restore.RelocateFiles.Add(dataFile);
restore.RelocateFiles.Add(logFile);
restore.SqlRestore(sqlServer);
restore.Devices.Remove(deviceItem);
}
我在 restore.SqlRestore(sqlServer)
处遇到错误:
Logical file 'mydbnew_log' is not part of database 'mydbnew'. Use RESTORE FILELISTONLY to list the logical file names.
RESTORE DATABASE is terminating abnormally.
我的代码有什么问题?
已解决。
我用这个版本更改了重定位部分:
RelocateFile dataFile = new RelocateFile();
dataFile.LogicalFileName = "mydb";
dataFile.PhysicalFileName = Path.Combine(@"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA", "mydbnew.mdf");
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = "mydb_log";
logFile.PhysicalFileName = Path.Combine(@"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA", "mydbnew.ldf");
当然,我必须努力动态获取路径和名称,但它确实有效。
看起来是 2 个问题,第一个:
RelocateFile logFile = new RelocateFile();
dataFile.LogicalFileName = databaseName + "_log";
dataFile.PhysicalFileName = databaseName + ".ldf";
您正在创建名称为 logFile
的对象,但在下一条语句中为旧变量设置值。
我想你希望它是:
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = databaseName + "_log";
logFile.PhysicalFileName = databaseName + ".ldf";
下一期,LogicalFileName
是原始数据库中的实际逻辑名。但是,PhysicalFileName
是 new 名称。
因此,例如,如果您的数据库是这样的,
那么代码是这样的:
RelocateFile dataFile = new RelocateFile();
dataFile.LogicalFileName = "Mine";
dataFile.PhysicalFileName = sourcePath + databaseName + ".mdf";
RelocateFile logFile = new RelocateFile();
logFile.LogicalFileName = "Mine_log";
logFile.PhysicalFileName = sourcePath + databaseName + ".ldf";
RelocateFile
使用旧的逻辑名称(原始数据库),但在 PhysicalFileName
以上代码使用新名称创建文件。
祝你好运。