【问题标题】:SMO "Restore failed for Server" Restoring Backup From FileSMO“恢复服务器失败”从文件恢复备份
【发布时间】:2010-10-20 01:22:31
【问题描述】:

我正在使用以下代码通过 C# 和 SMO 恢复备份 sql 数据库。

void RestoreDatabaseWithSMO(string sConnect, string dbName, string backUpPath)
{
    using (SqlConnection cnn = new SqlConnection(sConnect))
    {
        cnn.Open();
        cnn.ChangeDatabase("master");

        ServerConnection sc = new ServerConnection(cnn);
        Server sv = new Server(sc);

        if (!sv.Databases.Contains(dbName))
            throw new Exception("this DataBase does not exist");

        // Create backup device item for the backup
        BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);

        // Create the restore object
        Restore resDB = new Restore();
        resDB.PercentComplete += new PercentCompleteEventHandler(percentComplete);
        resDB.PercentCompleteNotification = 10;
        resDB.Devices.Add(bdi);
        resDB.NoRecovery = false;
        resDB.ReplaceDatabase = true;
        resDB.Database = dbName;
        resDB.Action = RestoreActionType.Database;

        // Restore the database
        resDB.SqlRestore(sv);//Exception
    }
}

但在最后一行我得到了以下异常!!!

{"Restore failed for Server '\\\\.\\pipe\\3F103E6E-3FD4-47\\tsql\\query'. "}

这有什么问题?
你能指导我吗?谢谢

【问题讨论】:

    标签: c# sql restore smo database-restore


    【解决方案1】:

    我已经用 T-SQL 完成了

    Exclusive access could not be obtained because the database is in use

    void Restore(string ConnectionString, string DatabaseFullPath, string backUpPath)
    {
        using (SqlConnection con = new SqlConnection(ConnectionString))
        {
            con.Open();
    
            string UseMaster = "USE master";
            SqlCommand UseMasterCommand = new SqlCommand(UseMaster, con);
            UseMasterCommand.ExecuteNonQuery();
    
            string Alter1 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Single_User WITH Rollback Immediate";
            SqlCommand Alter1Cmd = new SqlCommand(Alter1, con);
            Alter1Cmd.ExecuteNonQuery();
    
            string Restore = @"RESTORE DATABASE [" + DatabaseFullPath + "] FROM DISK = N'" + backUpPath + @"' WITH  FILE = 1,  NOUNLOAD,  STATS = 10";
            SqlCommand RestoreCmd = new SqlCommand(Restore, con);
            RestoreCmd.ExecuteNonQuery();
    
            string Alter2 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Multi_User";
            SqlCommand Alter2Cmd = new SqlCommand(Alter2, con);
            Alter2Cmd.ExecuteNonQuery();
    
            labelReport.Text = "Successful";
        }
    }
    

    【讨论】:

    • 我的猜测是因为它是对 SMO 问题的 T-SQL 答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2012-05-11
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多