Skip to main content
Email Password   helpLost your password?

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;
}
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalvery Coooooooooooool ! Pin
Namdar2002
21:11 16 Aug '09  
GeneralTry EZmanage SQL Pro Pin
itayl
4:13 27 May '09  
GeneralRestore problem Pin
ayman tawfik
21:56 24 May '09  
GeneralWorks great! Pin
Ed Brown 65
10:28 18 Mar '09  
GeneralThank You ;) Pin
saravanan.rex@gmail.com
21:11 13 Nov '08  
Generalthank you Pin
hsy11
3:25 9 Sep '08  
GeneralThank you Pin
Taheri6182
20:43 12 May '08  
GeneralBackup option Pin
Rakesh1611
6:33 6 Nov '07  
GeneralThis is just what the doctor ordered! Pin
Eric Murray
13:48 19 Mar '07  
GeneralSuper usefull!!! Thank's mate.;) Pin
OrmusDog
3:52 16 Dec '06  
GeneralYou're a Tool Pin
jberkhei
10:28 17 Nov '06  
GeneralThank you Pin
Gianluca Simionato
4:45 9 Oct '06  
QuestionSql Backup Pin
| Muhammad Waqas Butt |
0:49 9 Nov '05  
GeneralNice article, but this is possible without T-SQL Pin
Randy Friend
7:11 3 Nov '05  
GeneralRe: Nice article, but this is possible without T-SQL Pin
DeeJRoss
5:59 7 Apr '06  
GeneralRe: Nice article, but this is possible without T-SQL Pin
Polymorpher
6:46 1 Jun '06  


Last Updated 23 Oct 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009