新增管理页,数据库压缩,清理和备份

This commit is contained in:
FalconFly 2024-07-23 16:16:11 +08:00
parent e7f8aaed1f
commit a96b0329d3
4 changed files with 251 additions and 13 deletions

View File

@ -2,6 +2,6 @@
<configuration>
<appSettings>
<add key="saveFileName" value="data.db"/>
<add key ="sqliteFile" value="DataSource=work.db"/>
<add key ="sqliteFile" value="DataSource=work.db;"/>
</appSettings>
</configuration>

View File

@ -1,5 +1,7 @@
using CalendarNotepad.Models;
using SqlSugar;
using System.Text;
using System.Text.RegularExpressions;
namespace CalendarNotepad.Extends
{
@ -18,11 +20,55 @@ namespace CalendarNotepad.Extends
IsAutoCloseConnection = false,
};
/// <summary>
/// 初始化数据库
/// </summary>
public void DbInit() {
using var db = new DbContext();
db.CodeFirst.InitTables<WorkUnit>();
db.CodeFirst.InitTables<WorkUnit>();
db.CodeFirst.InitTables<PlusFileUnit>();
db.CodeFirst.InitTables<WorkUnitPlusFile>();
}
/// <summary>
/// 清除数据库垃圾
/// </summary>
public string DbClean() {
StringBuilder sb = new();
int i;
i = this.Deleteable<WorkUnitPlusFile>()
.Where(a => !SqlFunc.Subqueryable<WorkUnit>().Where(b => b.WorkDay == a.WorkDay).Any())
.ExecuteCommand();
sb.AppendLine($"WorkUnitPlusFile 清除{i}条数据。");
i = this.Deleteable<PlusFileUnit>()
.Where(a => !SqlFunc.Subqueryable<WorkUnitPlusFile>().Where(b => b.FileKey == a.FileId).Any())
.ExecuteCommand();
sb.AppendLine($"PlusFileUnit 清除{i}条数据。");
return sb.ToString();
}
/// <summary>
/// 压缩数据库文件
/// </summary>
/// <returns></returns>
public string DbVacuum() {
var file = this.SqliteDbfile;
var oldSize = new FileInfo(file).Length;
this.Ado.ExecuteCommand("VACUUM;");
var newSize = new FileInfo(file).Length;
return $"数据库压缩:原始{oldSize},压缩后{newSize}";
}
/// <summary>
/// sqllite数据库文件
/// </summary>
public string SqliteDbfile {
get {
var regex = new Regex(@"DataSource\s*=\s*(?<key>.*?);");
var mc = regex.Match(AppConfig.SqliteFileName);
var file = mc.Groups["key"].Value;
return file;
}
}
}
}

View File

@ -52,11 +52,23 @@
cms_FileList = new ContextMenuStrip(components);
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
toolStrip1 = new ToolStrip();
tsp_Copy = new ToolStripButton();
tsb_palse = new ToolStripButton();
tabPage1 = new TabPage();
flowLayoutPanel3 = new FlowLayoutPanel();
bt_m_ys = new Button();
label3 = new Label();
flowLayoutPanel2 = new FlowLayoutPanel();
bt_m_Clean = new Button();
label2 = new Label();
panel2 = new Panel();
panel3 = new Panel();
OpenFileDlg = new OpenFileDialog();
ToolStripMenuItem = new ToolStripMenuItem();
flowLayoutPanel4 = new FlowLayoutPanel();
label1 = new Label();
bt_m_Backup = new Button();
tabControl1.SuspendLayout();
tpWorkManage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
@ -70,11 +82,16 @@
C_WorkTypes.SuspendLayout();
cms_FileList.SuspendLayout();
toolStrip1.SuspendLayout();
tabPage1.SuspendLayout();
flowLayoutPanel3.SuspendLayout();
flowLayoutPanel2.SuspendLayout();
flowLayoutPanel4.SuspendLayout();
SuspendLayout();
//
// tabControl1
//
tabControl1.Controls.Add(tpWorkManage);
tabControl1.Controls.Add(tabPage1);
tabControl1.Dock = DockStyle.Fill;
tabControl1.Location = new Point(0,0);
tabControl1.Name = "tabControl1";
@ -329,22 +346,29 @@
//
cms_FileList.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem,ToolStripMenuItem,ToolStripMenuItem });
cms_FileList.Name = "contextMenuStrip1";
cms_FileList.Size = new Size(181,92);
cms_FileList.Size = new Size(101,70);
//
// 删除ToolStripMenuItem
//
ToolStripMenuItem.Name = "删除ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "删除";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 导入ToolStripMenuItem
//
ToolStripMenuItem.Name = "导入ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "导入";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 下载ToolStripMenuItem
//
ToolStripMenuItem.Name = "下载ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "下载";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// toolStrip1
//
toolStrip1.Items.AddRange(new ToolStripItem[] { tsp_Copy,tsb_palse });
@ -374,16 +398,139 @@
tsb_palse.Text = "粘贴";
tsb_palse.Click += tsb_palse_Click;
//
// tabPage1
//
tabPage1.Controls.Add(flowLayoutPanel4);
tabPage1.Controls.Add(flowLayoutPanel3);
tabPage1.Controls.Add(flowLayoutPanel2);
tabPage1.Controls.Add(panel2);
tabPage1.Controls.Add(panel3);
tabPage1.Location = new Point(4,26);
tabPage1.Name = "tabPage1";
tabPage1.Padding = new Padding(3);
tabPage1.Size = new Size(709,413);
tabPage1.TabIndex = 2;
tabPage1.Text = "管理";
tabPage1.UseVisualStyleBackColor = true;
//
// flowLayoutPanel3
//
flowLayoutPanel3.AutoSize = true;
flowLayoutPanel3.Controls.Add(bt_m_ys);
flowLayoutPanel3.Controls.Add(label3);
flowLayoutPanel3.Dock = DockStyle.Top;
flowLayoutPanel3.Location = new Point(3,42);
flowLayoutPanel3.Name = "flowLayoutPanel3";
flowLayoutPanel3.Padding = new Padding(5);
flowLayoutPanel3.Size = new Size(703,39);
flowLayoutPanel3.TabIndex = 4;
//
// bt_m_ys
//
bt_m_ys.Location = new Point(8,8);
bt_m_ys.Name = "bt_m_ys";
bt_m_ys.Size = new Size(75,23);
bt_m_ys.TabIndex = 0;
bt_m_ys.Text = "压缩";
bt_m_ys.UseVisualStyleBackColor = true;
bt_m_ys.Click += bt_m_ys_Click;
//
// label3
//
label3.AutoSize = true;
label3.Font = new Font("微软雅黑",12F,FontStyle.Regular,GraphicsUnit.Point);
label3.Location = new Point(89,5);
label3.Name = "label3";
label3.Size = new Size(266,21);
label3.TabIndex = 1;
label3.Text = "压缩数据库文件尺寸,减少文件大小";
//
// flowLayoutPanel2
//
flowLayoutPanel2.AutoSize = true;
flowLayoutPanel2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
flowLayoutPanel2.Controls.Add(bt_m_Clean);
flowLayoutPanel2.Controls.Add(label2);
flowLayoutPanel2.Dock = DockStyle.Top;
flowLayoutPanel2.Location = new Point(3,3);
flowLayoutPanel2.Name = "flowLayoutPanel2";
flowLayoutPanel2.Padding = new Padding(5);
flowLayoutPanel2.Size = new Size(703,39);
flowLayoutPanel2.TabIndex = 3;
//
// bt_m_Clean
//
bt_m_Clean.Location = new Point(8,8);
bt_m_Clean.Name = "bt_m_Clean";
bt_m_Clean.Size = new Size(75,23);
bt_m_Clean.TabIndex = 0;
bt_m_Clean.Text = "清理";
bt_m_Clean.UseVisualStyleBackColor = true;
bt_m_Clean.Click += bt_m_Clean_Click;
//
// label2
//
label2.AutoSize = true;
label2.Font = new Font("微软雅黑",12F,FontStyle.Regular,GraphicsUnit.Point);
label2.Location = new Point(89,5);
label2.Name = "label2";
label2.Size = new Size(170,21);
label2.TabIndex = 1;
label2.Text = "清除数据库中无用垃圾";
//
// panel2
//
panel2.AutoSize = true;
panel2.Dock = DockStyle.Top;
panel2.Location = new Point(3,3);
panel2.Name = "panel2";
panel2.Size = new Size(703,0);
panel2.TabIndex = 2;
//
// panel3
//
panel3.AutoSize = true;
panel3.Dock = DockStyle.Top;
panel3.Location = new Point(3,3);
panel3.Name = "panel3";
panel3.Size = new Size(703,0);
panel3.TabIndex = 1;
//
// OpenFileDlg
//
OpenFileDlg.Multiselect = true;
//
// 下载ToolStripMenuItem
// flowLayoutPanel4
//
ToolStripMenuItem.Name = "下载ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Text = "下载";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
flowLayoutPanel4.AutoSize = true;
flowLayoutPanel4.Controls.Add(bt_m_Backup);
flowLayoutPanel4.Controls.Add(label1);
flowLayoutPanel4.Dock = DockStyle.Top;
flowLayoutPanel4.Location = new Point(3,81);
flowLayoutPanel4.Name = "flowLayoutPanel4";
flowLayoutPanel4.Padding = new Padding(5);
flowLayoutPanel4.Size = new Size(703,39);
flowLayoutPanel4.TabIndex = 5;
//
// label1
//
label1.AutoSize = true;
label1.Font = new Font("微软雅黑",12F,FontStyle.Regular,GraphicsUnit.Point);
label1.Location = new Point(89,5);
label1.Name = "label1";
label1.Size = new Size(266,21);
label1.TabIndex = 1;
label1.Text = "备份数据文件,以便将来出错时还原";
//
// bt_m_Backup
//
bt_m_Backup.Location = new Point(8,8);
bt_m_Backup.Name = "bt_m_Backup";
bt_m_Backup.Size = new Size(75,23);
bt_m_Backup.TabIndex = 0;
bt_m_Backup.Text = "备份";
bt_m_Backup.UseVisualStyleBackColor = true;
bt_m_Backup.Click += bt_m_Backup_Click;
//
// WorkMain
//
@ -414,6 +561,14 @@
cms_FileList.ResumeLayout(false);
toolStrip1.ResumeLayout(false);
toolStrip1.PerformLayout();
tabPage1.ResumeLayout(false);
tabPage1.PerformLayout();
flowLayoutPanel3.ResumeLayout(false);
flowLayoutPanel3.PerformLayout();
flowLayoutPanel2.ResumeLayout(false);
flowLayoutPanel2.PerformLayout();
flowLayoutPanel4.ResumeLayout(false);
flowLayoutPanel4.PerformLayout();
ResumeLayout(false);
}
@ -449,5 +604,17 @@
private ToolStripMenuItem ToolStripMenuItem;
private OpenFileDialog OpenFileDlg;
private ToolStripMenuItem ToolStripMenuItem;
private TabPage tabPage1;
private Panel panel3;
private Label label3;
private Button bt_m_ys;
private FlowLayoutPanel flowLayoutPanel3;
private FlowLayoutPanel flowLayoutPanel2;
private Button bt_m_Clean;
private Label label2;
private Panel panel2;
private FlowLayoutPanel flowLayoutPanel4;
private Button bt_m_Backup;
private Label label1;
}
}

View File

@ -101,7 +101,7 @@ namespace CalendarNotepad
}
private void WorkMain_Load(object sender,EventArgs e) {
mcWorkDay_DateChanged(null,null);
mcWorkDay_DateChanged(sender,new DateRangeEventArgs(this.mcWorkDay.SelectionStart,this.mcWorkDay.SelectionEnd));
}
/// <summary>
@ -166,7 +166,7 @@ namespace CalendarNotepad
}
private void btReset_Click(object sender,EventArgs e) {
mcWorkDay_DateChanged(sender,null);
mcWorkDay_DateChanged(sender,new DateRangeEventArgs(this.mcWorkDay.SelectionStart,this.mcWorkDay.SelectionEnd));
this.DataReset();
}
@ -305,5 +305,30 @@ namespace CalendarNotepad
}
}
}
private void bt_m_ys_Click(object sender,EventArgs e) {
var r = this.Db.DbVacuum();
MessageBox.Show(r);
}
private void bt_m_Clean_Click(object sender,EventArgs e) {
var r = this.Db.DbClean();
MessageBox.Show(r);
}
private void bt_m_Backup_Click(object sender,EventArgs e) {
var sdlg = new SaveFileDialog {
Title = "备份数据库",
FileName = Path.GetFileName($"workdb.{DateTime.Now.ToString("yyyyMMdd")}.bak"),
};
var dresult = sdlg.ShowDialog();
if(dresult != DialogResult.OK) {
return;
}
var filePath = sdlg.FileName;
File.Copy(this.Db.SqliteDbfile,filePath);
MessageBox.Show("备份完成");
}
}
}