我有这个代码,使用MS Access作为数据库在C#中备份和恢复。我完成了zip格式的备份,现在我想恢复Zipped文件。任何帮助都感激不尽。
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());
}
}
此代码提取压缩文件,但我不知道如何同时进行恢复。
using (ZipFile zip = ZipFile.Read(restoreFile))
{
zip.ExtractAll(CurrentDatabasePath);
}
答案 0 :(得分:2)
我做到了!对于那些需要代码的人来说,这是:
using (ZipFile zip = ZipFile.Read(pathBackup))
{
zip.ExtractAll(Environment.CurrentDirectory, ExtractExistingFileAction.OverwriteSilently);
}
答案 1 :(得分:0)
在您积极使用数据库时,无法直接覆盖数据库
我主动认为你在该数据库上打开了OleDbConnection
。
从上面的代码中无法理解你是否处于这种情况,所以首先要做的是搜索OleDbConnection的所有出现并检查它们是否正确关闭。如果您有一个全局OleDbConnection,它在应用程序的生命周期内保持打开(这是一种非常糟糕的做法),那么您需要在尝试覆盖accdb文件之前将其关闭
public void RestoreDatabase(string restoreFile)
{
string dbFileName = "dbCPS.accdb";
string extractedFile = Path.GetTempFileName();
string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory, dbFileName);
using (ZipFile zip = ZipFile.Read(restoreFile))
{
// Extract to a temporary name built by the Framework for you....
zip.ExtractAll(extractedFile);
}
// Now, before copying over the accdb file, you need to be sure that there is no
// open OleDbConnection to this file, otherwise the IOException occurs because you
// cannot change that file while it is actively used by a OleDbConnection
// something like global_conn.Close(); global_conn.Dispose();
File.Copy(extractedFile, CurrentDatabasePath, true);
MessageBox.Show("Restore successful! ");
// and then reopen the connection
}
答案 2 :(得分:0)
private void btn_UodateMembers_Click(object sender, EventArgs e)
{
if (!bwUpdateMembers.IsBusy)
{
bwUpdateMembers.RunWorkerAsync();
}
}
private string ExtractZip(FileInfo fi)
{
string extractTo = Path.Combine(fi.DirectoryName, Guid.NewGuid().ToString());
using (ZipFile zip = ZipFile.Read(fi.FullName))
{
foreach (ZipEntry ze in zip)
{
ze.Extract(extractTo, ExtractExistingFileAction.OverwriteSilently);
}
}
return extractTo;
}
public FileInfo GetLatestFile(DirectoryInfo di)
{
FileInfo fi = di.GetFiles()
.OrderByDescending(d => d.CreationTime)
.FirstOrDefault();
return fi;
}
private void bwUpdateMembers_DoWork(object sender, DoWorkEventArgs e)
{
string path = "C:\\Users\\Ghost Wolf\\Desktop\\zip";
DirectoryInfo di = new DirectoryInfo(path);
if (di != null)
{
FileInfo fi = GetLatestFile(di);
string folder = ExtractZip(fi);
MessageBox.Show("Your'e Files Have Been Extracted", "Notice", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
请告诉我,如果这是为你工作!!