3
Reply

Restore database through c# codings

leathu raj

leathu raj

Oct 6 2014 8:16 AM
769
Hai,
   I want to restore database backup file(.bak) to sql server 2012 from my c# program. Restored database name is  from textbox text.
I used the following coding. It works on local connection only. when i used the same coding on my sql server connected with name like XXXX\SQL EXPRESS i got error restored failed for server 'XXXX\SQL EXPRESS'.
Please help me.... Thanks in advance....
 
ServerConnection srvConn = new ServerConnection(@"XXXXX\\SQLEXPRESS");
 
 instead of this line i use
 
ServerConnection srvConn = new ServerConnection("localhost"); 
this line work on loacl connection. 
 
 
 try
private static Server srvr;
ServerConnection srvConn = new ServerConnection(@"WIN-6A67QTTNU6J\\SQLEXPRESS");
srvConn.LoginSecure = true;
srvr = new Server(srvConn);
 
 
Restore rstDatabase = new Restore();
rstDatabase.Action = RestoreActionType.Database;
string name = txt_cmpnyname.Text;
rstDatabase.Database = name;
BackupDeviceItem bkpDevice = new BackupDeviceItem("E:\\shop", DeviceType.File);
rstDatabase.Devices.Add(bkpDevice);
rstDatabase.ReplaceDatabase = true;
rstDatabase.SqlRestore(server);
MessageBox.Show("Restored Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
 

Answers (3)
Next Recommended Forum