Click here to Skip to main content
13,055,958 members (47,658 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked
Posted 23 Oct 2005

Restore SQL Server Backup Files into any database

, 23 Oct 2005
Rate this:
Please Sign up or sign in to vote.
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.

Sample Image - Title.jpg


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:

   FROM DISK = 'C:\Northwind.BAK'
      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 = 
                                                      "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();
  return s;


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

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralRestore problem Pin
ayman tawfik24-May-09 20:56
memberayman tawfik24-May-09 20:56 
GeneralWorks great! Pin
Ed Brown 6518-Mar-09 9:28
memberEd Brown 6518-Mar-09 9:28 
GeneralThank You ;) Pin
saravanan.rex@gmail.com13-Nov-08 20:11
membersaravanan.rex@gmail.com13-Nov-08 20:11 
Generalthank you Pin
hsy119-Sep-08 2:25
memberhsy119-Sep-08 2:25 
GeneralThank you Pin
Taheri618212-May-08 19:43
memberTaheri618212-May-08 19:43 
GeneralBackup option Pin
Rakesh16116-Nov-07 5:33
memberRakesh16116-Nov-07 5:33 
GeneralThis is just what the doctor ordered! Pin
Eric Murray19-Mar-07 12:48
memberEric Murray19-Mar-07 12:48 
GeneralSuper usefull!!! Thank's mate.;) Pin
OrmusDog16-Dec-06 2:52
memberOrmusDog16-Dec-06 2:52 
GeneralYou're a Tool Pin
jberkhei17-Nov-06 9:28
memberjberkhei17-Nov-06 9:28 
GeneralThank you Pin
Gianluca Simionato9-Oct-06 3:45
memberGianluca Simionato9-Oct-06 3:45 
QuestionSql Backup Pin
| Muhammad Waqas Butt |8-Nov-05 23:49
member| Muhammad Waqas Butt |8-Nov-05 23:49 
GeneralNice article, but this is possible without T-SQL Pin
Randy Friend3-Nov-05 6:11
memberRandy Friend3-Nov-05 6:11 
GeneralRe: Nice article, but this is possible without T-SQL Pin
DeeJRoss7-Apr-06 4:59
memberDeeJRoss7-Apr-06 4:59 
GeneralRe: Nice article, but this is possible without T-SQL Pin
Polymorpher1-Jun-06 5:46
memberPolymorpher1-Jun-06 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 24 Oct 2005
Article Copyright 2005 by Phorozan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid