本章實作問卷系統的輔助功能:資料庫備份/還原、視窗管理等。核心邏輯極簡單——利用 File.Copy 複製 .mdb 檔案即可。
private void BakDbToolStripMenuItem_Click(object sender, EventArgs e)
{
string dbpath = @"..\..\DataBase\Lj_QuestionnaireSys.mdb";
string _dbpath = dbpath.Substring(dbpath.LastIndexOf('\\'));
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Access資料庫檔案(*.mdb)|*.mdb";
sfd.Title = "請選擇備份儲存路徑";
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
File.Copy(dbpath, sfd.FileName, true);
MessageBox.Show("匯出成功!請妥善保管備份檔。", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("匯出失敗!請重試。", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void RDbToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("還原將覆蓋現有資料庫且無法復原,確定繼續?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
string dbpath = @"..\..\DataBase\Lj_QuestionnaireSys.mdb";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Access資料庫檔案(*.mdb)|*.mdb";
ofd.Title = "請選擇要還原的資料庫檔";
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
File.Copy(ofd.FileName, dbpath, true);
MessageBox.Show("還原成功!請重新整理列表。", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("還原失敗!請確認備份檔是否正確。", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
同理可擴充「視窗排列」、「使用者偏好設定」等輔助功能,提升系統易用性。