Click here to Skip to main content
Licence 
First Posted 23 Oct 2005
Views 101,770
Downloads 2,438
Bookmarked 66 times

Restore SQL Server Backup Files into any database

By Phorozan | 23 Oct 2005
When you create a backup, you must be able to restore this backup into the same database and same location. But this code also helps you to restore a backup into a new database or existing database.
 
Part of The SQL Zone sponsored by
See Also
5 votes, 17.9%
1

2
1 vote, 3.6%
3
6 votes, 21.4%
4
16 votes, 57.1%
5
4.68/5 - 28 votes
5 removed
μ 3.82, σa 2.68 [?]

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


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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGR8 PinmemberVishweshwar Ballary1:02 22 Mar '11  
GeneralMy vote of 5 PinmemberVishweshwar Ballary1:02 22 Mar '11  
GeneralMy vote of 5 PinmemberShahin Khorshidnia17:22 31 Dec '10  
Generalvery Coooooooooooool ! PinmemberNamdar200221:11 16 Aug '09  
GeneralTry EZmanage SQL Pro Pinmemberitayl4:13 27 May '09  
GeneralRestore problem Pinmemberayman tawfik21:56 24 May '09  
GeneralWorks great! PinmemberEd Brown 6510:28 18 Mar '09  
GeneralThank You ;) Pinmembersaravanan.rex@gmail.com21:11 13 Nov '08  
Generalthank you Pinmemberhsy113:25 9 Sep '08  
GeneralThank you PinmemberTaheri618220:43 12 May '08  
GeneralBackup option PinmemberRakesh16116:33 6 Nov '07  
GeneralThis is just what the doctor ordered! PinmemberEric Murray13:48 19 Mar '07  
GeneralSuper usefull!!! Thank's mate.;) PinmemberOrmusDog3:52 16 Dec '06  
GeneralYou're a Tool Pinmemberjberkhei10:28 17 Nov '06  
GeneralThank you PinmemberGianluca Simionato4:45 9 Oct '06  
QuestionSql Backup Pinmember| Muhammad Waqas Butt |0:49 9 Nov '05  
GeneralNice article, but this is possible without T-SQL PinmemberRandy Friend7:11 3 Nov '05  
This is good information, but it is possible to restore any MS SQL database over another DB or to a new DB without using T-SQL. To do so, do the following...
 
Start SQL Enterprise Manager
Expand the Servers node
Expand the Server Group node containing the desired server
Expand the Server node
Expand the Databases node
 
If you want to restore to a new DB, right click on the 'Databases' entry and create a new database.
 
Right click on the DB where you want the restore to go
Select All Tasks | Restore Database...
 
On the Restore Database form...
Select From Device
Select the 'Select Devices...' button
Select the 'Add' button
Browse to and select the database file to restore
Select OK
Select OK
Select the Options Tab
Check the 'Force restore over existing database' option
Change the path and file name to be the database to be restored for both the MDF and LDF.
Select OK
 

GeneralRe: Nice article, but this is possible without T-SQL PinmemberDeeJRoss5:59 7 Apr '06  
GeneralRe: Nice article, but this is possible without T-SQL PinmemberPolymorpher6:46 1 Jun '06  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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