2

我正在尝试使用C# 中的文件BackupRestore我的数据库。.MDF我认为它是备份,因为我看到了一个.BAK文件,但是当我恢复时,它不起作用。两者在运行时都没有错误,但我认为我的代码有问题。这是我的代码:

    public void BackupDatabase(String databaseName, String destinationPath)
    {

        ServerConnection connection = new ServerConnection(conn);
        Backup sqlBackup = new Backup();                    

        sqlBackup.Action = BackupActionType.Database;
        sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
                                         DateTime.Now.ToShortDateString();
        sqlBackup.BackupSetName = "Archive";

        sqlBackup.Database = databaseName;

        BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases[databaseName];

        sqlBackup.Initialize = true;
        sqlBackup.Checksum = true;
        sqlBackup.ContinueAfterError = true;

        sqlBackup.Devices.Add(deviceItem);
        sqlBackup.Incremental = false;

        sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

        sqlBackup.FormatMedia = false;

        sqlBackup.SqlBackup(sqlServer);
    }

    public void RestoreDatabase(String databaseName, String filePath, String dataFilePath, String logFilePath)
    {
            ServerConnection connection = new ServerConnection(conn);
            Restore sqlRestore = new Restore();
            BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File);
            sqlRestore.Devices.Add(deviceItem);
            sqlRestore.Database = databaseName;

            Server sqlServer = new Server(connection);

            Database db = sqlServer.Databases[databaseName];
            sqlRestore.Action = RestoreActionType.Database;
            String dataFileLocation = dataFilePath + databaseName + ".mdf";
            String logFileLocation = logFilePath + databaseName + "_Log.ldf";
            db = sqlServer.Databases[databaseName];
            RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);

            sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
            sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName+"_log", logFileLocation));
            sqlRestore.ReplaceDatabase = true;

            sqlRestore.SqlRestore(sqlServer);
            db = sqlServer.Databases[databaseName];
            db.SetOnline();
            sqlServer.Refresh();
    }

    private void backupToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        saveFileDialogBackUp.FileName = fileName;// +".bak";
        saveFileDialogBackUp.Filter = "Backup File (*.bak)|*.bak";
        saveFileDialogBackUp.Title = "File Location";
        saveFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Sauvegardes";
            if (saveFileDialogBackUp.ShowDialog() == DialogResult.OK)
            {
            BackupDatabase("dbCPS", saveFileDialogBackUp.FileName);
            MessageBox.Show("Backup Successful!", "CPS Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
    }

    private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
    {
        openFileDialogBackUp.Filter = "Backup File (*.bak)|*.bak";
        openFileDialogBackUp.FileName = "CPS";
        openFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Sauvegardes";
        if (openFileDialogBackUp.ShowDialog() == DialogResult.OK)
        {
            RestoreDatabase("dbCPS", openFileDialogBackUp.FileName, openFileDialogBackUp.FileName, openFileDialogBackUp.FileName);
            MessageBox.Show("Database Restored", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }                       
    }
4

0 回答 0