May 19, 2005 at 7:52 am
How to restore a database backup to a specified databse with a different name (and show a progress bar) using SQLDMO
and c#?
any help will be appreciated
May 20, 2005 at 1:22 pm
why wouldn't you just use enterprise manager?
this works fine for me.
//create objects
SQLDMO.SQLServer2 s = new SQLDMO.SQLServer2Class();
SQLDMO.Backup2 b = new SQLDMO.Backup2Class();
SQLDMO.Restore2 r = new SQLDMO.Restore2Class();
//db name
string d = "empty";
//db owner - needed to get the old logical file names below
string o = "dbo";
//backup file
string f = "[c:/"+d+".bak]";
//connect to server
s.LoginSecure = true;
s.Connect(".",null,null);
//backup the database named 'empty'
b.Database = d;
b.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
b.Files = f;
b.SQLBackup(s);
//restore database to 'empty_copy'
r.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
r.Database = d+"_copy";
r.Files = f;
r.ReplaceDatabase = true;
//note how we get the names of the old logical files here and move the physical location to the root of c
r.RelocateFiles =
"["+s.Databases.Item(d,o).EnumFiles().GetColumnString(1,8).ToString()+"],[c:\\"+d+"_copy_data.mdf],"+
"["+s.Databases.Item(d,o).EnumFiles().GetColumnString(2,8).ToString()+"],[c:\\"+d+"_copy_log.ldf]";
r.SQLRestore(s);
print("restored");
May 20, 2005 at 1:23 pm
progress bar would require a lot more effort. you're on your own there =P
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply