0

根据这篇文章,我制作了一个小型 WPF 应用程序来备份/恢复数据库。

代码:

using System.Windows;
using Microsoft.SqlServer.Management.Smo;

namespace DBManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private Server srv;
        private Database db;
        private BackupDeviceItem bdi;
        private int recoverymod;

        public MainWindow()
        {
            InitializeComponent();
            srv = new Server();
            db = srv.Databases["MyDB"];
            recoverymod = (int) db.DatabaseOptions.RecoveryModel;
            bdi = new BackupDeviceItem("Test_Full_Backup1", DeviceType.File);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BackupDB();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            RestoreDB();
        }

        public void BackupDB()
        {
            var bk = new Backup
            {
                Action = BackupActionType.Database,
                BackupSetDescription = "Full backup of MyDB",
                BackupSetName = "MyDB Backup",
                Database = "MyDB"
            };

            bk.Devices.Add(bdi);
            bk.Incremental = false;
            bk.LogTruncation = BackupTruncateLogType.Truncate;
            bk.SqlBackup(srv);
            label1.Content = "Backup finished";
            bk.Devices.Remove(bdi);
        }

        public void RestoreDB()
        {
            db.Drop();
            var rs = new Restore { NoRecovery = true };
            rs.Devices.Add(bdi);
            rs.Database = "MyDB";
            rs.SqlRestore(srv);
            label1.Content = "Restoration finished";
            db = srv.Databases["MyDB"];
            rs.Devices.Remove(bdi);
            db.RecoveryModel = (RecoveryModel) recoverymod;
        }
    }
}

当我单击 button1 时,备份文件按预期创建。

当我单击 button2 时,我可以看到“恢复完成”消息,一切看起来都不错,但是我无法使用另一个使用它的应用程序访问数据库。

在 SQL Management Studio 中,我在数据库名称后看到“(正在恢复...)”,并且看起来恢复过程永远不会完成。

Visual Studio 2010 和 SQL Server 2012

4

1 回答 1

0

数据库处于恢复状态,因为您指定了:

NoRecovery = true

如果您希望在还原后恢复数据库,请将此行更改为:

NoRecovery = false

此处Comparison of RECOVERY and NORECOVERY的部分中的更多信息。

于 2013-08-26T16:19:37.153 回答