![]() |
Database »
Database »
SQL Server
Intermediate
Restore SQL Server Backup Files into any databaseBy PhorozanWhen 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. |
C#, SQL.NET1.1, Win2K, WinXP, Win2003, Visual-Studio, SQL2000, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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.
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. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Oct 2005 Editor: Smitha Vijayan |
Copyright 2005 by Phorozan Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |