Click here to Skip to main content
15,997,596 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Some One Please Help Me
When i Am trying to Restore a database it gives me error-

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


C#
private void button1_Click(object sender, EventArgs e)
   {
       BackupDB(@"C:\TempDB_Jaggry.bak");
       RestoreDB(@"C:\TempDB_Jaggry.bak", "DB_Jaggry");
   }


Code for Backup-

C#
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());
    }           
 }


Code For Restore-
C#
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());
    }        
}


Thnk you In Advance...
Posted
Updated 12-Sep-12 20:01pm
v2

 
Share this answer
 
Hi,

you can also try with the direct SQL query for back up and restoring the databases. you simply execute that queries using MASTER database with ExecuteNonQuery Method of SqlCommand Class.

check these below links.
Backing up to a disk device[^]
SQL SERVER – Restore Database Backup using SQL Script (T-SQL)[^]

hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900