Click here to Skip to main content
Click here to Skip to main content

Restore SQL Server Backup Files into any database

By , 23 Oct 2005
 

Sample Image - Title.jpg

Introduction

When a backup is created from a Microsoft SQL Server database, by default it must restore the same database at the same location. But if you want to restore this backup at another location in another server you must use customized T-SQL scripts. This operation consumes a lot of time.

Customize T-SQL Backup Statements

Now you can use customized T-SQL statements to restore a database in any location. For example:

RESTORE DATABASE NewNorthwind
   FROM DISK = 'C:\Northwind.BAK'
   WITH 
      MOVE 'Northwind_Data' TO 'C:\NewNorthwind_Data.mdf' ,
      MOVE 'Northwind_Log'  TO 'C:\NewNorthwind_log.ldf', REPLACE

This script must be generated for each database.

Load T-SQL Statements from Assembly

We can store T-SQL statements in an Exe or a DLL file by adding a new file to the project and setting Build Action property to Embedded Resource. For example, add a new file with the name Restore.sql to a project and set the Build Action property. Now for loading it from the assembly, use the function:

private string LoadSQLFromAssembly (string Name)
{
  System.IO.Stream stream = 
    this.GetType().Assembly.GetManifestResourceStream(this.GetType(), 
                                                      "SQL." + Name);

  if(stream == null)
  {
    MessageBox.Show("Internal Error occured! Close Application" + 
      " & try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return null;
  }

  System.IO.StreamReader reader= new System.IO.StreamReader(stream);

  if (reader == null)
  {
    MessageBox.Show("Internal Error occured! Close Application" + 
      " & try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return null;
  }

  string s = reader.ReadToEnd();
  reader.Close();
  return s;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Phorozan
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNetwork restorin not workingmemberveena nair19 Mar '13 - 21:47 
i downloaded this project. it is working successfully on the local machine. It can select a backup file from the local machine and can restore it on the same machine.
 
But when we are selecting a backup from the local machine and trying to restore it in a remote machine, it is showing an file could not found and some 'smo' errors... why is it please help...
thanks in advance....
GeneralMy vote of 5memberReza Alipour Fard3 Jan '13 - 2:04 
استاد مقالتون حرف نداره.
مثل همیشه بی نقص هستش.
GeneralMy vote of 5mvpKanasz Robert24 Sep '12 - 6:10 
Good article
GeneralMy vote of 5memberMs komika battranah16 Jul '12 - 2:37 
May GOD Bless You
Evry Time & Evry Work
GeneralMy vote of 5memberm-mousavi1 Jun '12 - 1:44 
Excellent
GeneralGR8memberVishweshwar Ballary22 Mar '11 - 0:02 
Thumbs Up | :thumbsup:
GeneralMy vote of 5memberVishweshwar Ballary22 Mar '11 - 0:02 
Xellent
GeneralMy vote of 5memberShahin Khorshidnia31 Dec '10 - 16:22 
Not bad ,
 
But good picture !
Generalvery Coooooooooooool !memberNamdar200216 Aug '09 - 20:11 
Thumbs Up | :thumbsup: Big Grin | :-D
thanks Mr.Phorozan
its very useful code
GeneralTry EZmanage SQL Promemberitayl27 May '09 - 3:13 
Try EZmanage SQL Pro for better mssql backup
 
EZManage SQL Pro provids 5 time faster sql backup using VDI
 
http://www.futureitsoft.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 24 Oct 2005
Article Copyright 2005 by Phorozan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid