还原数据库时出现问题. [英] Problem In Restoring The dabase.

查看:125
本文介绍了还原数据库时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试还原数据库时,它给我错误-

When I am Trying to restore database it''s gives me error-

"Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
code for restore-
My Code For Restoring DB is-


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());
            }
        }

<big>plz help me how to write a code in c# to restore the database by closing all the connection of the database.</big>

推荐答案

以下应该有效:
仅当数据库没有任何连接时,才可以进行还原.在MS SQL Server上将所有用户踢掉的简单方法是:
Following should work:
A restore can only happen if the database does not have any connections to it. The easy way on a MS SQL Server to kick all users off is:
ALTER DATABASE [MyDB] SET Single_User WITH Rollback Immediate
GO


现在,您可以不受惩罚地执行还原.完成还原后,请确保将其设置回多用户模式:


Now, you can perform your restore with impunity. Make sure you set it back to Multi-user mode when you''re done with the restore:

ALTER DATABASE [MyDB] SET Multi_User
GO


此处引用完全相同的问题:专有由于正在使用数据库,因此无法获得访问权限 [ ^ ]


Referenced from exact same issue here: Exclusive access could not be obtained because the database is in use[^]




您是否检查过此博客:正在修复数据库正在使用错误从备份还原数据库时 [
Hi,

Have you checked this blog : Fixing database is in use error while restore database from backup[^]

You need to make your database offline before backup.


这篇关于还原数据库时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆