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);
}
Renish PatelPosted Oct 8, 2014, 5:24 AM
If you found solution then please mark as answered.
Thanks
Renish Patel
leathu rajPosted Oct 8, 2014, 4:35 AM
i change my codings as below i will restore the databse. the mistake is not in localconnection. mistake is to restore the already existing database with different name.
Renish PatelPosted Oct 7, 2014, 8:37 AM
I share nice link for your reference,Please follow it.
http://manish4dotnet.blogspot.in/2013/03/RestoreDataBaseusingCSharp.html
Please mark this question as answer if it solve your problem
Happy Coding
Regards,
Renish