简体   繁体   English

使用C#显示错误还原Sqlserver数据库

[英]Restore Sqlserver Database Using C# Showing Error

I am trying to backup and restore sql server 2008r2 database, and i successfully backup my database , But the problem is when i am trying to restore my DB its showing the following error Restore failed for Server 'XXXX-PC\\SQLEXPRESS' 我正在尝试备份和恢复sql server 2008r2数据库,并且我成功备份了我的数据库,但问题是当我尝试恢复我的数据库时显示以下错误服务器'XXXX-PC \\ SQLEXPRESS'恢复失败

And Another important thing is that , When i Close the DB Connection (From server Explorer [right-click->Close Connection]) its working Normally And Correctly 另一个重要的事情是,当我关闭数据库连接时(从服务器资源管理器[右键单击 - >关闭连接]),它的工作正常和正确

Below is my Backup And Restore Code Please help me to fix the problem 以下是我的备份和还原代码请帮我解决问题

public static void BackupDatabase(string backUpFile)
{
    ServerConnection con = new ServerConnection(@"XXXX-PC\SQLEXPRESS");
    Server server = new Server(con);
    Backup source = new Backup();
    source.Action = BackupActionType.Database;
    source.Database = "testBD";
    BackupDeviceItem destination = new BackupDeviceItem(backUpFile, DeviceType.File);
    source.Devices.Add(destination);
    //source.Devices.AddDevice(@"G:\MyBackUp.bak", DeviceType.File);
    source.SqlBackup(server);
    con.Disconnect();
}

public static void RestoreDatabase(string backUpFile)
{
    ServerConnection con = new ServerConnection(@"XXXX-PC\SQLEXPRESS");
    Server server = new Server(con);
    Restore destination = new Restore();
    destination.Action = RestoreActionType.Database;
    destination.Database = "testBD";
    BackupDeviceItem source = new BackupDeviceItem(backUpFile, DeviceType.File);

    destination.Devices.Add(source);

    destination.ReplaceDatabase = true;

    con.Disconnect();
    server.ConnectionContext.Disconnect();

    destination.SqlRestore(server);
}

This is the error : 这是错误: 在此输入图像描述

This is the error message in detail: 这是错误消息:

在此输入图像描述

My guess is that your restore process can't get exclusive access to the database. 我的猜测是你的恢复过程无法获得对数据库的独占访问权限。 The Server SMO object has a KillAllProcesses method that takes a database as an argument and does what it says on the tin. Server SMO对象有一个KillAllProcesses方法 ,该方法将数据库作为参数并执行它在锡上所说的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM