제 질문은 기본적으로 제목에 명시되어 있습니다 만, 아쉽게도 확장하려고합니다. 데이터베이스를 백업 한 다음 해당 데이터베이스를 동일한 서버에있는 다른 데이터베이스로 복원하려고합니다. 지금은 테스트 목적으로 같은 서버에 보관하고 있지만 새 서버로 복원해야 할 수도 있습니다.SMO 복원이 항상 작동하지 않는 경우
코드는 작동하지만 오류는 없습니다. 그러나 한 번 실행하면 복원 된 데이터베이스가 업데이트된다는 보장이 없습니다. 만약 내가 그것을 실행하면 5 번 말할 수 있습니다, 그것은 가장 가능성이 데이터베이스를 업데이 트됩니다.
왜 지금 업데이트되지 않는지 궁금합니다.
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
// This program is used to perform a full database backup. The code also includes a SMO restore as well
namespace BackupDB
{
class Program
{
static void Main(string[] args)
{
// Connecting to the server
ServerConnection serverConn = new ServerConnection(@"T520-R9K0H1K\SQLEXPRESS");
Server server = new Server(serverConn);
backup(serverConn, server);
restore(serverConn, server);
Console.WriteLine("Closing Application");
Environment.Exit(0);
} // Main
public static void backup(ServerConnection serverConn, Server server)
{
if (!serverConn.IsOpen)
{
serverConn.Connect();
}
Backup bkpDBFull = new Backup();
bkpDBFull.Action = BackupActionType.Database;
bkpDBFull.Database = "databaseTest";
BackupDeviceItem bkpDevice = new BackupDeviceItem(@"C:\BakFiles\test.bak", DeviceType.File);
bkpDBFull.Devices.Add(bkpDevice);
bkpDBFull.BackupSetName = "databaseTest Backup";
bkpDBFull.BackupSetDescription = " databaseTest - Full Backup";
bkpDBFull.NoRecovery = false;
bkpDBFull.Initialize = true;
try
{
bkpDBFull.SqlBackupAsync(server);
Console.WriteLine("Backup Complete");
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.Write(ex.Source);
}
finally{ serverConn.Disconnect(); }
} // Backup
public static void restore(ServerConnection serverConn, Server server)
{
if (!serverConn.IsOpen)
{
serverConn.Connect();
}
Restore destination = new Restore();
destination.Action = RestoreActionType.Database;
destination.ReplaceDatabase = true;
destination.Database = "RestoreDB";
BackupDeviceItem source = new BackupDeviceItem(@"C:\BakFiles\test.bak", DeviceType.File);
destination.Devices.Add(source);
destination.NoRecovery = false;
destination.RelocateFiles.Add(new RelocateFile("databaseTest", @"C:\Restored\test.mdf"));
destination.RelocateFiles.Add(new RelocateFile("databaseTest_Log", @"C:\Restored\test_log.ldf"));
try
{
destination.SqlRestoreAsync(server);
Console.WriteLine("Restore Complete");
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.Write(ex.Source);
}
finally{ serverConn.Disconnect(); }
} // Restore
} // Class
} // Namespace
정말 고마워요! 이 솔루션은 완벽하게 작동합니다! –