Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                Server s = new Server(@"PARDEEP-PC\SQLEXPRESS");
                s.ConnectionContext.LoginSecure = true;
                s.ConnectionContext.Connect();


                Restore restoreDB = new Restore();
                restoreDB.Database = "MyNewDatabase";
                
                /* Specify whether you want to restore database or files or log etc */
                restoreDB.Action = RestoreActionType.Database;
                restoreDB.Devices.AddDevice(@"D:\newDatabaseBackup.bak", DeviceType.File);

                /* You can specify ReplaceDatabase = false (default) to not create a new
                 * database, the specified database must exist on SQL Server instance.
                 * You can specify ReplaceDatabase = true to create new database 
                 * regardless of the existence of specified database */
                restoreDB.ReplaceDatabase = true;

                /* If you have a differential or log restore to be followed, you would 
                 * specify NoRecovery = true, this will ensure no recovery is done
                 * after the restore and subsequent restores are completed. The database
                 * would be in a recovered state. */
                restoreDB.NoRecovery = false;

                /* RelocateFiles collection allows you to specify the logical file names
                 * and physical file names (new locations) if you want to restore to a
                 * different location.*/

                restoreDB.RelocateFiles.Add(new RelocateFile("MyNewDatabaseNew", @"E:\MyNewDatabase_Data.mdf"));
                restoreDB.RelocateFiles.Add(new RelocateFile("MyNewDatabaseNew", @"E:\MyNewDatabase_Log.ldf"));

                /* Wiring up events for progress monitoring */
                //restoreDB.PercentComplete += CompletionStatusInPercent;
                //restoreDB.Complete += Restore_Completed;

                /* SqlRestore method starts to restore database
                 * You can also use SqlRestoreAsync method to perform restore 
                 * operation asynchronously */
                restoreDB.SqlRestore(s);
                MessageBox.Show(":)");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


This code work fine for restoring database from backup file, if i remove these two lines from the code
restoreDB.RelocateFiles.Add(new RelocateFile("MyNewDatabaseNew", @"E:\MyNewDatabase_Data.mdf"));
             restoreDB.RelocateFiles.Add(new RelocateFile("MyNewDatabaseNew", @"E:\MyNewDatabase_Log.ldf"));


is there any error in relocating these files
Posted
Comments
Sandeep Mewara 4-Oct-12 15:58pm    
What error?
footballpardeep 4-Oct-12 16:02pm    
Restore failed for server (it's name)

1 solution

I use the tsql Restore Database command, it is not dependent on the SQL dll's : http://msdn.microsoft.com/en-us/library/ms186858.aspx[^]
 
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