Commit c6479c38 authored by 朱天博's avatar 朱天博

1.类生成工具

parent 18f89659
......@@ -72,6 +72,12 @@
<DependentUpon>wEditBasic.cs</DependentUpon>
</Compile>
<Compile Include="MyData\DBHelper.cs" />
<Compile Include="UI\wAutoClass.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\wAutoClass.Designer.cs">
<DependentUpon>wAutoClass.cs</DependentUpon>
</Compile>
<Compile Include="UI\Models\AppModel.cs" />
<Compile Include="MyCommon\Common.cs" />
<Compile Include="MyCommon\wBasic.cs">
......@@ -184,6 +190,9 @@
<EmbeddedResource Include="UI\wAppModelEdit.resx">
<DependentUpon>wAppModelEdit.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UI\wAutoClass.resx">
<DependentUpon>wAutoClass.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UI\wDbAnlysis.resx">
<DependentUpon>wDbAnlysis.cs</DependentUpon>
</EmbeddedResource>
......
namespace Tools.UI
{
partial class wAutoClass
{
/// <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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(wAutoClass));
this.tstbLinkString = new System.Windows.Forms.ToolStripTextBox();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.tsbDB = new System.Windows.Forms.ToolStripButton();
this.tsbRefresh = new System.Windows.Forms.ToolStripButton();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.lsbTableInfo = new System.Windows.Forms.ListBox();
this.txtSearch = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.toolStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// tstbLinkString
//
this.tstbLinkString.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F);
this.tstbLinkString.ForeColor = System.Drawing.Color.Green;
this.tstbLinkString.Name = "tstbLinkString";
this.tstbLinkString.ReadOnly = true;
this.tstbLinkString.Size = new System.Drawing.Size(150, 25);
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsbDB,
this.tstbLinkString,
this.tsbRefresh});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(710, 25);
this.toolStrip1.TabIndex = 5;
this.toolStrip1.Text = "toolStrip1";
//
// tsbDB
//
this.tsbDB.Image = global::Tools.Properties.Resources.数据库;
this.tsbDB.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tsbDB.Name = "tsbDB";
this.tsbDB.Size = new System.Drawing.Size(64, 22);
this.tsbDB.Text = "数据库";
this.tsbDB.Click += new System.EventHandler(this.tsbDB_Click);
//
// tsbRefresh
//
this.tsbRefresh.Image = global::Tools.Properties.Resources.更新;
this.tsbRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tsbRefresh.Name = "tsbRefresh";
this.tsbRefresh.Size = new System.Drawing.Size(88, 22);
this.tsbRefresh.Text = "刷新表数据";
this.tsbRefresh.Click += new System.EventHandler(this.tsbRefresh_Click);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location = new System.Drawing.Point(0, 25);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.lsbTableInfo);
this.splitContainer1.Panel1.Controls.Add(this.txtSearch);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.txtResult);
this.splitContainer1.Size = new System.Drawing.Size(710, 439);
this.splitContainer1.SplitterDistance = 168;
this.splitContainer1.TabIndex = 7;
//
// lsbTableInfo
//
this.lsbTableInfo.Dock = System.Windows.Forms.DockStyle.Fill;
this.lsbTableInfo.FormattingEnabled = true;
this.lsbTableInfo.ItemHeight = 12;
this.lsbTableInfo.Location = new System.Drawing.Point(0, 21);
this.lsbTableInfo.Name = "lsbTableInfo";
this.lsbTableInfo.Size = new System.Drawing.Size(168, 418);
this.lsbTableInfo.TabIndex = 8;
this.lsbTableInfo.SelectedIndexChanged += new System.EventHandler(this.lsbTableInfo_SelectedIndexChanged);
//
// txtSearch
//
this.txtSearch.Dock = System.Windows.Forms.DockStyle.Top;
this.txtSearch.Location = new System.Drawing.Point(0, 0);
this.txtSearch.Name = "txtSearch";
this.txtSearch.Size = new System.Drawing.Size(168, 21);
this.txtSearch.TabIndex = 7;
this.txtSearch.TextChanged += new System.EventHandler(this.txtSearch_TextChanged);
this.txtSearch.Enter += new System.EventHandler(this.txtSearch_Enter);
this.txtSearch.Leave += new System.EventHandler(this.txtSearch_Leave);
//
// txtResult
//
this.txtResult.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtResult.Location = new System.Drawing.Point(0, 0);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtResult.Size = new System.Drawing.Size(538, 439);
this.txtResult.TabIndex = 0;
//
// wAutoClass
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(710, 464);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.toolStrip1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "wAutoClass";
this.Text = "类生成工具";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ToolStripTextBox tstbLinkString;
private System.Windows.Forms.ToolStripButton tsbDB;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.TextBox txtSearch;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.ToolStripButton tsbRefresh;
private System.Windows.Forms.ListBox lsbTableInfo;
}
}
\ 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.MyCommon;
using Tools.MyData;
using Tools.MyUtils;
namespace Tools.UI
{
public partial class wAutoClass : wEditBasic
{
DataTable _dataTable;
public wAutoClass()
{
InitializeComponent();
}
private void tsbDB_Click(object sender, EventArgs e)
{
var frm = new wDbSetting(this.tstbLinkString);
if (frm.ShowDialog() == DialogResult.OK)
{
//初始化数据访问对象
this.InitDBHelper(frm.LinkModel);
RefrashData();
}
}
private void tsbRefresh_Click(object sender, EventArgs e)
{
RefrashData();
}
private void RefrashData(bool _reLoadData = true)
{
if (string.IsNullOrEmpty(this.tstbLinkString.Text))
return;
if (_reLoadData)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("SELECT object_id,name FROM sys.objects WHERE type_desc ='USER_TABLE' ORDER BY name");
_dataTable = this._db.GetDataTable(sb.ToString(), "结果列表");
}
if (txtSearch.Text == "--搜索--")
return;
lsbTableInfo.Items.Clear();
foreach (var row in _dataTable.Select($"name like '%{txtSearch.Text}%'").OrderBy(p => p["name"]))
lsbTableInfo.Items.Add(row["name"].ToString());
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.tstbLinkString.Text) || txtSearch.ForeColor == Color.LightGray)
return;
this.RefrashData(false);
}
Boolean textboxHasText = false;//判断输入框是否有文本
private void txtSearch_Enter(object sender, EventArgs e)
{
if (textboxHasText == false)
txtSearch.Text = "";
txtSearch.ForeColor = Color.Black;
}
private void txtSearch_Leave(object sender, EventArgs e)
{
if (txtSearch.Text == "")
{
txtSearch.Text = "--搜索--";
txtSearch.ForeColor = Color.LightGray;
textboxHasText = false;
}
else
textboxHasText = true;
}
private void lsbTableInfo_SelectedIndexChanged(object sender, EventArgs e)
{
string strTableName = lsbTableInfo.SelectedItem.ToString();
StringBuilder sb = new StringBuilder();
sb.AppendLine($@"
SELECT b.name,b.user_type_id,c.name as TypeName FROM sys.objects a
INNER JOIN sys.columns b ON b.object_id = a.object_id
INNER JOIN sys.types c ON b.user_type_id=c.user_type_id
WHERE a.name='{strTableName}' ORDER BY b.column_id
");
DataTable dataTable = this._db.GetDataTable(sb.ToString(), "结果列表");
sb.Clear();
sb.Append($@"
/// <summary>
/// {strTableName}
/// </summary>
public class {strTableName}
{{
");
foreach(DataRow row in dataTable.Rows)
{
sb.Append($@"
/// <summary>
/// {row["name"]}
/// </summary>
public {GetCsharpTypeNameBySqlType(row["TypeName"].ToString())} {row["name"]} {{ get; set; }}
");
}
sb.Append($@"
}}
");
txtResult.Text = sb.ToString();
}
private string GetCsharpTypeNameBySqlType(string nSqlTypeName)
{
switch (nSqlTypeName)
{
case "image": return "Image";
case "int": return "int";
case "datetime": return "DateTime";
case "bit": return "bool";
case "varchar": return "string";
case "char": return "char";
default:return "string";
}
}
}
}
This diff is collapsed.
......@@ -28,6 +28,7 @@ namespace Tools
this.CreateModule(typeof(UI.wMonitorVisit), "APP模块访问监控"),
this.CreateModule(typeof(UI.wAppModel), "APP模块管理"),
this.CreateModule(typeof(UI.wSysModule), "功能模块管理"),
this.CreateModule(typeof(UI.wAutoClass), "类生成工具"),
};
}
......
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