Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL database
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 4-Oct-12 10:06am
Comments
Sandeep Mewara at 4-Oct-12 15:58pm
   
What error?
footballpardeep at 4-Oct-12 16:02pm
   
Restore failed for server (it's name)

1 solution

Rate this: bad
good
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 : http://msdn.microsoft.com/en-us/library/ms186858.aspx[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
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