Click here to Skip to main content
15,173,905 members
Articles / Web Development / HTML
Article
Posted 23 Oct 2005

Stats

155.4K views
4.9K downloads
74 bookmarked

Restore SQL Server Backup Files into any database

Rate me:
Please Sign up or sign in to vote.
4.80/5 (34 votes)
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.

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:

SQL
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:

C#
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

Share

About the Author

Phorozan
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Questiongreat work Pin
Member 1383865822-May-18 1:04
MemberMember 1383865822-May-18 1:04 
QuestionNetwork restorin not working Pin
veena nair19-Mar-13 22:47
Memberveena nair19-Mar-13 22:47 
GeneralMy vote of 5 Pin
Reza Alipour Fard3-Jan-13 3:04
MemberReza Alipour Fard3-Jan-13 3:04 
GeneralMy vote of 5 Pin
Kanasz Robert24-Sep-12 7:10
professionalKanasz Robert24-Sep-12 7:10 
GeneralMy vote of 5 Pin
Ms komika battranah16-Jul-12 3:37
MemberMs komika battranah16-Jul-12 3:37 
GeneralMy vote of 5 Pin
m-mousavi1-Jun-12 2:44
Memberm-mousavi1-Jun-12 2:44 
Excellent
GeneralGR8 Pin
Vishweshwar Ballary22-Mar-11 1:02
MemberVishweshwar Ballary22-Mar-11 1:02 
GeneralMy vote of 5 Pin
Vishweshwar Ballary22-Mar-11 1:02
MemberVishweshwar Ballary22-Mar-11 1:02 
GeneralMy vote of 5 Pin
Shahin Khorshidnia31-Dec-10 17:22
professionalShahin Khorshidnia31-Dec-10 17:22 
Generalvery Coooooooooooool ! Pin
Namdar200216-Aug-09 21:11
MemberNamdar200216-Aug-09 21:11 
GeneralTry EZmanage SQL Pro Pin
itayl27-May-09 4:13
Memberitayl27-May-09 4:13 
GeneralRestore problem Pin
ayman tawfik24-May-09 21:56
Memberayman tawfik24-May-09 21:56 
GeneralWorks great! Pin
EB6518-Mar-09 10:28
MemberEB6518-Mar-09 10:28 
GeneralThank You ;) Pin
Saravanan.rex13-Nov-08 21:11
MemberSaravanan.rex13-Nov-08 21:11 
Generalthank you Pin
hsy119-Sep-08 3:25
Memberhsy119-Sep-08 3:25 
GeneralThank you Pin
Taheri618212-May-08 20:43
MemberTaheri618212-May-08 20:43 
GeneralBackup option Pin
Rakesh16116-Nov-07 6:33
MemberRakesh16116-Nov-07 6:33 
GeneralThis is just what the doctor ordered! Pin
Eric Murray19-Mar-07 13:48
MemberEric Murray19-Mar-07 13:48 
GeneralSuper usefull!!! Thank's mate.;) Pin
OrmusDog16-Dec-06 3:52
MemberOrmusDog16-Dec-06 3:52 
GeneralYou're a Tool Pin
jberkhei17-Nov-06 10:28
Memberjberkhei17-Nov-06 10:28 
GeneralThank you Pin
Gianluca Simionato9-Oct-06 4:45
MemberGianluca Simionato9-Oct-06 4:45 
QuestionSql Backup Pin
| Muhammad Waqas Butt |9-Nov-05 0:49
professional| Muhammad Waqas Butt |9-Nov-05 0:49 
GeneralNice article, but this is possible without T-SQL Pin
Randy Friend3-Nov-05 7:11
MemberRandy Friend3-Nov-05 7:11 
GeneralRe: Nice article, but this is possible without T-SQL Pin
Ross Peoples7-Apr-06 5:59
MemberRoss Peoples7-Apr-06 5:59 
GeneralRe: Nice article, but this is possible without T-SQL Pin
Polymorpher1-Jun-06 6:46
MemberPolymorpher1-Jun-06 6: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.