System.Data.SqlClient.SqlException: Logical file DB_Jaggry is not part of database DB_Jaggry_Restore&. Use RESTORE FILELISTONLY to list the logical file names.<br /> RESTORE DATABASE is terminating abnormally
private void button1_Click(object sender, EventArgs e) { BackupDB(@"C:\TempDB_Jaggry.bak"); RestoreDB(@"C:\TempDB_Jaggry.bak", "DB_Jaggry"); }
public static void BackupDB(string backupDestinationFilePath) { try { // Console.WriteLine("Backup operation started"); Backup backup = new Backup(); //Set type of backup to be performed to database backup.Action = BackupActionType.Database; backup.BackupSetDescription = "BackupDataBase description"; //Set the name used to identify a particular backup set. backup.BackupSetName = "Backup"; //specify the name of the database to back up backup.Database = "DB_Jaggry"; //Set up the backup device to use filesystem. BackupDeviceItem deviceItem = new BackupDeviceItem( backupDestinationFilePath, DeviceType.File); backup.Devices.Add(deviceItem); // Setup a new connection to the data server ServerConnection connection = new ServerConnection(); Server sqlServer = new Server(@"SNEHA-PC\SQLEXPRESS"); //Initialize devices associated with a backup operation. backup.Initialize = true; backup.Checksum = true; backup.ContinueAfterError = true; backup.LogTruncation = BackupTruncateLogType.Truncate; backup.SqlBackup(sqlServer); MessageBox.Show("Backup operation succeeded"); } catch (Exception ex) { //Console.WriteLine("Backup operation failed"); // Console.WriteLine(ex.Message); MessageBox.Show(ex.ToString()); } }
public static void RestoreDB(string backUpFilePath, string databaseName) { try { Restore restore = new Restore(); restore.Database = databaseName; restore.Action = RestoreActionType.Database; restore.Devices.AddDevice(backUpFilePath, DeviceType.File); restore.ReplaceDatabase = true; restore.NoRecovery = false; restore.RelocateFiles.Add(new RelocateFile("DB_Jaggry",@"C:\Temp\DB_jaggry.mdf")); restore.RelocateFiles.Add(new RelocateFile("DB_Jaggry_Log",@"C:\Temp\DB_Jaggry_Log.ldf")); ServerConnection connection = new ServerConnection(@"SNEHA-PC\SQLEXPRESS"); Server sqlServer = new Server(connection); restore.SqlRestore(sqlServer); MessageBox.Show("Restore operation succeeded"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
MASTER
ExecuteNonQuery
SqlCommand
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)