Commit e078c297 authored by tr's avatar tr

架构规划:采用统一父类约定

parent 9a81d3ca
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.MyCommon
{
public interface IEditForm
{
void InitData();
void InitUI();
bool SaveCheck();
bool Save();
}
}
namespace Tools.MyCommon
{
partial class wEditBasic
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// wEditBasic
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(384, 268);
this.Name = "wEditBasic";
this.Text = "wEditBasic";
this.ResumeLayout(false);
}
#endregion
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using Tools.MyData;
using Tools.MyUtils;
namespace Tools.MyCommon
{
/// <summary>
/// 通用的编辑窗口父类
/// </summary>
public partial class wEditBasic : wBasic, IEditForm
{
/// <summary>
/// 数据访问对象
/// </summary>
public DBHelper _db = null;
/// <summary>
/// 全局数据集
/// </summary>
public DataSet _ds = null;
/// <summary>
/// 构造函数
/// </summary>
public wEditBasic()
{
InitializeComponent();
//窗体的基础初始化
this.Load += WEditBasic_Load;
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WEditBasic_Load(object sender, EventArgs e)
{
this.InitData();
this.InitUI();
}
/// <summary>
/// 初始化数据库访问对象
/// </summary>
/// <param name="link">链接字符串</param>
public virtual void InitDBHelper(string link)
{
if (string.IsNullOrEmpty(link))
{
throw new Exception("数据库链接为空,请检查!");
}
this._db = new DBHelper(link);
if (!this._db.ConnectionTest())
{
this._db = null;
throw new Exception($"数据库链接无效!\r\n链接:{link}");
}
}
/// <summary>
/// 初始化数据库访问对象
/// </summary>
/// <param name="m">数据库链接模型</param>
public virtual void InitDBHelper(DbItemModel m)
{
if (m == null)
{
throw new Exception("数据库链接模型为空,请检查!");
}
//调用本地初始化
this.InitDBHelper(m.LinkString);
}
/// <summary>
/// 初始化数据
/// </summary>
public virtual void InitData()
{
//将数据装入全局数据集_ds
}
/// <summary>
/// 初始化界面
/// </summary>
public virtual void InitUI()
{
//界面初始化,包括渲染和事件绑定
}
/// <summary>
/// 保存前检查
/// </summary>
/// <returns></returns>
public virtual bool SaveCheck()
{
//保存之前的检查,全部放到此方法处理
return true;
}
/// <summary>
/// 保存
/// </summary>
/// <returns></returns>
public virtual bool Save()
{
//保存的逻辑放到此方法内实现
return true;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
\ No newline at end of file
...@@ -12,7 +12,7 @@ namespace Tools.MyData ...@@ -12,7 +12,7 @@ namespace Tools.MyData
/// <summary> /// <summary>
/// 数据访问帮助类 /// 数据访问帮助类
/// </summary> /// </summary>
public class DatabaseHelper : IDisposable public class DBHelper : IDisposable
{ {
/// <summary> /// <summary>
/// 数据库访问对象 /// 数据库访问对象
...@@ -27,7 +27,7 @@ namespace Tools.MyData ...@@ -27,7 +27,7 @@ namespace Tools.MyData
/// 构造函数:数据访问帮助类 /// 构造函数:数据访问帮助类
/// </summary> /// </summary>
/// <param name="link">链接</param> /// <param name="link">链接</param>
public DatabaseHelper(string link) public DBHelper(string link)
{ {
this._link = link; this._link = link;
} }
...@@ -35,7 +35,7 @@ namespace Tools.MyData ...@@ -35,7 +35,7 @@ namespace Tools.MyData
/// 构造函数:数据访问帮助类 /// 构造函数:数据访问帮助类
/// </summary> /// </summary>
/// <param name="m"></param> /// <param name="m"></param>
public DatabaseHelper(DbItemModel m) public DBHelper(DbItemModel m)
{ {
this._link = m.LinkString; this._link = m.LinkString;
} }
......
...@@ -20,6 +20,10 @@ namespace Tools.MyUtils ...@@ -20,6 +20,10 @@ namespace Tools.MyUtils
/// 数据库链接字符串 /// 数据库链接字符串
/// </summary> /// </summary>
public DbItemModel LinkModel { get; private set; } public DbItemModel LinkModel { get; private set; }
/// <summary>
/// 回显的工具栏标签
/// </summary>
public ToolStripLabel EchoToolStripLabel { get; private set; }
SmpDB _db = new SmpDB(); SmpDB _db = new SmpDB();
DataTable _dt = null; DataTable _dt = null;
...@@ -30,12 +34,14 @@ namespace Tools.MyUtils ...@@ -30,12 +34,14 @@ namespace Tools.MyUtils
string UserId => this.tbUserId.Text.Trim(); string UserId => this.tbUserId.Text.Trim();
string Pwd => this.tbPwd.Text.Trim(); string Pwd => this.tbPwd.Text.Trim();
public wDbSetting() public wDbSetting(ToolStripLabel toolStripLabel = null)
{ {
InitializeComponent(); InitializeComponent();
this.MinimizeBox = false; this.MinimizeBox = false;
this.MaximizeBox = false; this.MaximizeBox = false;
this.DialogResult = DialogResult.None; this.DialogResult = DialogResult.None;
//设置回显的工具栏标签
this.EchoToolStripLabel = toolStripLabel;
} }
private void wDbSetting_Load(object sender, EventArgs e) private void wDbSetting_Load(object sender, EventArgs e)
{ {
...@@ -67,9 +73,14 @@ namespace Tools.MyUtils ...@@ -67,9 +73,14 @@ namespace Tools.MyUtils
this.LinkModel = new DbItemModel { Link = this.Link, DbName = this.DbName, UserId = this.UserId, Pwd = this.Pwd }; this.LinkModel = new DbItemModel { Link = this.Link, DbName = this.DbName, UserId = this.UserId, Pwd = this.Pwd };
if (!this._db.ConnectionTest(this.LinkModel.LinkString)) if (!this._db.ConnectionTest(this.LinkModel.LinkString))
{ {
Utils.Tip("数据库链接无效请检查!"); Utils.Tip("数据库链接无效,请检查!");
return; return;
} }
//设置回显
if (this.EchoToolStripLabel != null)
{
this.EchoToolStripLabel.Text = this.LinkModel.ToString();
}
//保存历史 //保存历史
var drArray = this._dt.Select($"Link='{this.Link}' and DbName='{this.DbName}' and UserId='{this.UserId}' and Pwd='{this.Pwd}'"); var drArray = this._dt.Select($"Link='{this.Link}' and DbName='{this.DbName}' and UserId='{this.UserId}' and Pwd='{this.Pwd}'");
if (drArray.Length == 0) if (drArray.Length == 0)
......
...@@ -64,7 +64,14 @@ ...@@ -64,7 +64,14 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="MyData\DatabaseHelper.cs" /> <Compile Include="MyCommon\IEditForm.cs" />
<Compile Include="MyCommon\wEditBasic.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MyCommon\wEditBasic.Designer.cs">
<DependentUpon>wEditBasic.cs</DependentUpon>
</Compile>
<Compile Include="MyData\DBHelper.cs" />
<Compile Include="UI\Models\AppModel.cs" /> <Compile Include="UI\Models\AppModel.cs" />
<Compile Include="MyCommon\Common.cs" /> <Compile Include="MyCommon\Common.cs" />
<Compile Include="MyCommon\wBasic.cs"> <Compile Include="MyCommon\wBasic.cs">
...@@ -143,6 +150,9 @@ ...@@ -143,6 +150,9 @@
<EmbeddedResource Include="MyCommon\wBasic.resx"> <EmbeddedResource Include="MyCommon\wBasic.resx">
<DependentUpon>wBasic.cs</DependentUpon> <DependentUpon>wBasic.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Include="MyCommon\wEditBasic.resx">
<DependentUpon>wEditBasic.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="MyUtils\wDbSetting.resx"> <EmbeddedResource Include="MyUtils\wDbSetting.resx">
<DependentUpon>wDbSetting.cs</DependentUpon> <DependentUpon>wDbSetting.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
......
...@@ -231,7 +231,6 @@ ...@@ -231,7 +231,6 @@
this.Controls.Add(this.toolStrip1); this.Controls.Add(this.toolStrip1);
this.Name = "wDbAnlysis"; this.Name = "wDbAnlysis";
this.Text = "简易SQL效率分析"; this.Text = "简易SQL效率分析";
this.Load += new System.EventHandler(this.wDbAnlysis_Load);
this.toolStrip1.ResumeLayout(false); this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout(); this.toolStrip1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel1.ResumeLayout(false);
......
...@@ -8,37 +8,32 @@ using System.Text; ...@@ -8,37 +8,32 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
// //
using Tools.MyCommon;
using Tools.MyData; using Tools.MyData;
using Tools.MyUtils; using Tools.MyUtils;
using System.Collections.Generic; using System.Collections.Generic;
namespace Tools.UI namespace Tools.UI
{ {
public partial class wDbAnlysis : MyCommon.wBasic public partial class wDbAnlysis : wEditBasic
{ {
/// <summary>
/// 数据库访问对象
/// </summary>
SmpDB _db = new SmpDB();
/// <summary>
/// 数据库链接
/// </summary>
string _link = "";
public wDbAnlysis() public wDbAnlysis()
{ {
InitializeComponent(); InitializeComponent();
} }
private void wDbAnlysis_Load(object sender, EventArgs e) /// <summary>
/// 初始化界面
/// </summary>
public override void InitUI()
{ {
//调用父级初始化界面
base.InitUI();
//工具栏-事件 //工具栏-事件
this.tsbAnlysis.Click += TsbAnlysis_Click; this.tsbAnlysis.Click += TsbAnlysis_Click;
this.tsbDbLink.Click += TsbDbLink_Click; this.tsbDbLink.Click += TsbDbLink_Click;
} }
/// <summary> /// <summary>
/// 事件:查询效率分析 /// 事件:查询效率分析
/// </summary> /// </summary>
...@@ -46,7 +41,7 @@ namespace Tools.UI ...@@ -46,7 +41,7 @@ namespace Tools.UI
/// <param name="e"></param> /// <param name="e"></param>
private void TsbAnlysis_Click(object sender, EventArgs e) private void TsbAnlysis_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(this._link)) if (this._db == null)
{ {
Utils.Tip("请先选择数据库!"); Utils.Tip("请先选择数据库!");
return; return;
...@@ -67,7 +62,7 @@ namespace Tools.UI ...@@ -67,7 +62,7 @@ namespace Tools.UI
SELECT COUNT(*) FROM {objName} SELECT COUNT(*) FROM {objName}
END"; END";
stopwatch.Restart(); stopwatch.Restart();
this._db.ExecNonSql(this._link, new List<string> { anlysisSql }); this._db.ExecNonSql(new List<string> { anlysisSql });
stopwatch.Stop(); stopwatch.Stop();
result.Add($"{stopwatch.ElapsedMilliseconds / 1000F}秒\t{objName}"); result.Add($"{stopwatch.ElapsedMilliseconds / 1000F}秒\t{objName}");
} }
...@@ -86,11 +81,11 @@ namespace Tools.UI ...@@ -86,11 +81,11 @@ namespace Tools.UI
/// <param name="e"></param> /// <param name="e"></param>
private void TsbDbLink_Click(object sender, EventArgs e) private void TsbDbLink_Click(object sender, EventArgs e)
{ {
var w = new wDbSetting(); var w = new wDbSetting(this.tslbDbLinkString);
if (w.ShowDialog() == DialogResult.OK) if (w.ShowDialog() == DialogResult.OK)
{ {
this._link = w.LinkModel.LinkString; //初始化数据库访问对象
this.tslbDbLinkString.Text = w.LinkModel.ToString(); this.InitDBHelper(w.LinkModel);
} }
} }
...@@ -103,7 +98,7 @@ namespace Tools.UI ...@@ -103,7 +98,7 @@ namespace Tools.UI
{ {
var result = new List<string>(); var result = new List<string>();
//只按常用连接字符分段 //只按常用连接字符分段
var partList = text.Split(new string[] { "from", "left join", "inner join" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var partList = text.Split(new string[] { " from ", " left join ", " inner join " }, StringSplitOptions.RemoveEmptyEntries).ToList();
partList.RemoveAt(0); //剔除第1个,第一个为select partList.RemoveAt(0); //剔除第1个,第一个为select
foreach (var s in partList) foreach (var s in partList)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment