Click here to Skip to main content
12,698,179 members (22,669 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# SQL database
private void button4_Click(object sender, EventArgs e)
                Server s = new Server(@"PARDEEP-PC\SQLEXPRESS");
                s.ConnectionContext.LoginSecure = true;

                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 */
            catch (Exception ex)

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 4-Oct-12 10:06am
Sandeep Mewara 4-Oct-12 15:58pm
What error?
footballpardeep 4-Oct-12 16:02pm
Restore failed for server (it's name)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I use the tsql Restore Database command, it is not dependent on the SQL dll's :[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100