我有一个 SQL 2008 数据库。我正在运行一个支持该数据库的表单,然后尝试更新它。如果更新失败,想法是恢复该备份。这是我用来恢复备份的代码。
public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String userName, String password)
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
sqlRestore.Action = RestoreActionType.Database;
string logFile = System.IO.Path.GetDirectoryName(backUpFile);
logFile = System.IO.Path.Combine(logFile, databaseName + "_Log.ldf");
string dataFile = System.IO.Path.GetDirectoryName(backUpFile);
dataFile = System.IO.Path.Combine(dataFile, databaseName + ".mdf");
Database db = sqlServer.Databases[databaseName];
RelocateFile rf = new RelocateFile(databaseName, dataFile);
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFile));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFile));
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
问题似乎是我选择的文件名与在线数据库不同。我基本上想用备份替换服务器上的数据库。调用 SqlRestore 时出现异常。
主要的例外说
{"服务器 'localhost' 恢复失败。"}
深入研究内部异常会显示这些错误
执行 Transact-SQL 语句或批处理时发生异常。
进而
Logical file 'DB' is not part of database 'DB'. Use RESTORE FILELISTONLY to list the logical file names.\r\nRESTORE DATABASE is terminating abnormally.
I assume there is some way to tell this to just use replace the existing DB as is.
I use this bit of code to get the file path of the DB to have a directory to dump the backup. Maybe this could be used to get the file names to recreate.
public string GetDBFilePath(String databaseName, String userName, String password, String serverName)
{
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
return sqlServer.Databases[databaseName].PrimaryFilePath;
}