Click here to Skip to main content
15,939,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have used two buttons one for Backup and other for Restore


private void button1_Click(object sender, EventArgs e)
{
            con = new SqlConnection("Data Source = localhost; Initial Catalog=master ;Integrated Security = True;");
            con.Open();
            if (con.State == ConnectionState.Closed)
                con.Open();
            string qry = "Restore  database Peshawar_Children_Academy_Database from Disk= 'F:\\My.bak'";
            com = new SqlCommand("use master", con);
            com.ExecuteNonQuery();
            com = new SqlCommand(qry, con);
            com.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Has been restored database", "Restoration", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button2_Click(object sender, EventArgs e)
 {
            con = new SqlConnection("Data Source=localhost;Initial Catalog=Peshawar_Children_Academy_DataBase;Integrated Security = True;");
            con.Open();
            string qry = "backup database Peshawar_Children_Academy_Database to disk ='F:\\My.bak' with init,stats=10";
            com = new SqlCommand(qry, con);
            com.ExecuteNonQuery();
            
            con.Close();
            con.Dispose();
            MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
}


Problem is when I click 1st Restore button then Restoration of Database has taken successfully but if I click 1st backup button and then click on Restore button then I got the following error Message.
Kindly help me and provide a better soution as soon as possible..Thanks
Message:
Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
Posted
Updated 22-Jul-10 18:55pm
v2

1 solution

Has your backup completed before you start a restore?
This could be why you are getting this error message.
 
Share this answer
 
Comments
Leo_Kjan 23-Jul-10 1:07am    
No the Backup completes successfully after that when i click Restore button then i got the error message..
No this does't solve my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900