jitendra yadav

jitendra yadav

  • NA
  • 11
  • 2k

Service-based Database in visual studio 2010

Feb 27 2015 5:40 AM

Hello developers,
Actually my problem is, I made a project in c# with service-based Database. Everything is going good. But I am stuck with backup and Restore process.
I have done Backup and I got databasename.bak file But I don't know how to Restore that BAK file in service-based Database.
If possible please tell me that is it possible and if it is then please tell me how to do that. Is there any third-party software that I have to use or is it done by Code.
 
if (sqlcon.State != ConnectionState.Open)
sqlcon.Open();


string destinationdb = Application.StartupPath + "\\Database1.mdf";
string query = "USE [master]; RESTORE DATABASE [" + destinationdb + "] FROM DISK = N'" + "d:\\svBackUp1.bak" + " ' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10";
SqlCommand cmd = new SqlCommand(query, sqlcon);
int b = cmd.ExecuteNonQuery();
if (b> 0)
{
}

this is the code which I am using for restore but I got error..

"Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
Changed database context to 'master'."

Not Resolve... Please Give me another solution.....
Thanks