📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DbBackUp
{
public partial class Form1 : Form
{
private BackUp backUp;
private FileInfo[] info;
public Form1()
{
InitializeComponent();
this.InitBackUp();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void InitBackUp()
{
string pathDb = Application.StartupPath + "\\Data\\LabAnimal_Data.MDF";
string pathLog = Application.StartupPath + "\\Data\\LabAnimal_Data_log.ldf";
string pathBackDb = Application.StartupPath + "\\BackUp\\LabAnimal_Data.MDF";
string pathBackUpLog = Application.StartupPath + "\\BackUp\\LabAnimal_Data_log.ldf";
this.backUp = new BackUp(pathDb, pathLog, pathBackDb, pathBackUpLog);
}
private void btBackUp_Click(object sender, EventArgs e)
{
try
{
info = this.backUp.BackUpData();
if (info == null)
{
return;
}
this.WriteBackUpLog();
MessageBox.Show("数据库备份成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("数据库备份失败!" + ex.Message, "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
SysLog.WriteLog("错误信息:"+ex.Message);
return;
}
}
private void btReduction_Click(object sender, EventArgs e)
{
try
{
info = this.backUp.ReductionData();
if (info == null)
{
return;
}
this.WriteReductionLog();
MessageBox.Show("数据库还原成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("数据库还原失败!" + ex.Message, "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
SysLog.WriteLog("错误信息:" + ex.Message);
return;
}
}
private void WriteBackUpLog()
{
rcLog.Clear();
rcLog.AppendText("数据库备份成功!\n");
rcLog.AppendText("要备份的数据库时间为:"+info[0].LastWriteTime.ToLongDateString() +" "+ info[0].LastWriteTime.ToLongTimeString() + "\n");
rcLog.AppendText("数据库备份到:\n");
rcLog.AppendText(info[2].FullName + "\n");
rcLog.AppendText(info[3].FullName);
SysLog.WriteLog(rcLog);
}
private void WriteReductionLog()
{
rcLog.Clear();
rcLog.AppendText("数据库还原成功!\n");
rcLog.AppendText("数据库还原时间为:" + info[2].LastWriteTime.ToLongDateString() + " " + info[2].LastWriteTime.ToLongTimeString() + "\n");
rcLog.AppendText("数据库文件还原到:\n");
rcLog.AppendText(info[0].FullName + "\n");
rcLog.AppendText(info[1].FullName);
SysLog.WriteLog(rcLog);
}
private void 查看日志ToolStripMenuItem_Click(object sender, EventArgs e)
{
SysLog.OpenLog();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -