c# - Can't restore database from backup file -
based on this article made small wpf application backup/restore database.
code:
using system.windows; using microsoft.sqlserver.management.smo; namespace dbmanager { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow { private server srv; private database db; private backupdeviceitem bdi; private int recoverymod; public mainwindow() { initializecomponent(); srv = new server(); db = srv.databases["mydb"]; recoverymod = (int) db.databaseoptions.recoverymodel; bdi = new backupdeviceitem("test_full_backup1", devicetype.file); } private void button1_click(object sender, routedeventargs e) { backupdb(); } private void button2_click(object sender, routedeventargs e) { restoredb(); } public void backupdb() { var bk = new backup { action = backupactiontype.database, backupsetdescription = "full backup of mydb", backupsetname = "mydb backup", database = "mydb" }; bk.devices.add(bdi); bk.incremental = false; bk.logtruncation = backuptruncatelogtype.truncate; bk.sqlbackup(srv); label1.content = "backup finished"; bk.devices.remove(bdi); } public void restoredb() { db.drop(); var rs = new restore { norecovery = true }; rs.devices.add(bdi); rs.database = "mydb"; rs.sqlrestore(srv); label1.content = "restoration finished"; db = srv.databases["mydb"]; rs.devices.remove(bdi); db.recoverymodel = (recoverymodel) recoverymod; } } }
when click button1 backup file created expected.
when click button2 can see "restoration finished" message , looks ok can't access database application uses it.
in sql management studio see "(restoring...)" after database's name , looks restoration process never finish.
visual studio 2010 , sql server 2012
the database left in recovering state because specified:
norecovery = true
if want database recovered after restore, change line to:
norecovery = false
more info in comparison of recovery , norecovery
section here.
Comments
Post a Comment