首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

.NET Windows 程式實戰(八):資料庫匯出還原與視窗管理輔助功能

     編輯:關於ASP.NET

本章實作問卷系統的輔助功能:資料庫備份/還原、視窗管理等。核心邏輯極簡單——利用 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);
            }
        }
    }
}

同理可擴充「視窗排列」、「使用者偏好設定」等輔助功能,提升系統易用性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved