Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i’m doing restore DB then following error occur,Please help me to resolve this error.
Exclusive access could not be obtained because the database is in use.RESTORE DATABASE is Terminating Abnormally

My Code For Restoring DB is-
C#
public static void RestoreDB(string backUpFilePath, string databaseName)
        {
            try
            {
                Restore restore = new Restore();
                restore.Database = databaseName;

                restore.Action = RestoreActionType.Database;
                restore.Devices.AddDevice(backUpFilePath, DeviceType.File);
                restore.ReplaceDatabase = true;
                restore.NoRecovery = false;
                restore.RelocateFiles.Add(new RelocateFile("DB_Jaggery", @"C:\Temp\DB_jaggry.mdf"));
                restore.RelocateFiles.Add(new RelocateFile("DB_Jaggery_log", @"C:\Temp\DB_Jaggry_log.ldf"));
                ServerConnection connection = new ServerConnection(@"SNEHA-PC\SQLEXPRESS");
                Server sqlServer = new Server(connection);
                restore.SqlRestore(sqlServer);
                MessageBox.Show("Restore operation succeeded");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

Thank u in Advance..
Posted
Updated 14-Sep-12 7:44am
v2

1 solution

 
Share this answer
 

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