简体   繁体   English

如何在C#winForms中还原MS Access数据库zip文件?

[英]How to Restore MS Access database zip file in C# winForms?

I have this backup and restore code in C# using MS Access as its database. 我使用MS Access作为数据库在C#中具有此备份和还原代码。 I finished doing the backup in zip format and now I want to restore the Zipped file. 我已经完成了zip格式的备份,现在我想恢复压缩文件。 Any help will be much appreciated. 任何帮助都感激不尽。

public void BackupDatabase(string dateToday)
    {

        string dbFileName = "dbCPS.accdb";
            string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory , dbFileName);
            string backTimeStamp = Path.GetFileNameWithoutExtension(dbFileName) + "_" + dateToday + ".zip";// +Path.GetExtension(dbFileName);
            string destFileName = backTimeStamp;// +dbFileName;
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                string PathtobackUp = fbd.SelectedPath.ToString();
                destFileName = Path.Combine(PathtobackUp, destFileName);

                //File.Copy(CurrentDatabasePath, destFileName, true);
                using (var zip = new ZipFile())
                {
                    zip.AddFile(dbFileName);
                    zip.Save(destFileName);
                }
                MessageBox.Show("Backup successful! ");                  
            }            
    }

private void backupToolStripMenuItem1_Click(object sender, EventArgs e)
    {            
        BackupDatabase(DateTime.Now.ToString("ddMMMyyyy_HH.mm"));
    }


public void RestoreDatabase(string restoreFile)
    {
        string dbFileName = "dbCPS.accdb";
        string pathBackup = restoreFile;
        string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory, dbFileName);
        File.Copy(pathBackup, CurrentDatabasePath, true);
        MessageBox.Show("Restore successful! "); 
    }

private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {                
            openFileDialogBackUp.FileName = "dbCPS";
            openFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Sauvegardes";
            if (openFileDialogBackUp.ShowDialog() == DialogResult.OK)
                RestoreDatabase(openFileDialogBackUp.FileName);
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }
    }

This code extracts the zipped file but I dont know how to do the restore at the same time. 这段代码提取了压缩文件,但我不知道如何同时进行还原。

        using (ZipFile zip = ZipFile.Read(restoreFile))
        {
            zip.ExtractAll(@"C:\Users\Desktop\backup");
        }

According to the MSDN documentation here , you should be able to extract the zip file directly to the destination folder. 根据此处的MSDN文档,您应该能够将zip文件直接提取到目标文件夹中。 Your existing "restore" code simply does a file copy to CurrentDatabasePath , so one would expect that a statement like... 您现有的“还原”代码只是将文件复制到CurrentDatabasePath ,因此人们希望这样的语句:

System.IO.Compression.ZipFile.ExtractToDirectory(pathToZipFile, CurrentDatabasePath);

...would be all it takes. ...将是全部。 (I would test it myself, but ZipFile was apparently added in .NET 4.5 and my copy of Visual Studio is too old.) (我会自己进行测试,但是ZipFile显然是在.NET 4.5中添加的,而我的Visual Studio副本太旧了。)

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

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