From 283b2e142eddcd49d019097ad1d0e674359ca38f Mon Sep 17 00:00:00 2001 From: forget-the-bright Date: Tue, 25 Jul 2023 09:18:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DatabasetoEntityclass/App.config | 14 +- .../DatabasetoEntityclass.csproj | 46 ++++- DatabasetoEntityclass/DbHelper.cs | 24 +-- DatabasetoEntityclass/Form1.Designer.cs | 97 ++++++++++- DatabasetoEntityclass/Form1.cs | 162 +++++++++++++++++- DatabasetoEntityclass/SystemConfig.xml | 4 +- 6 files changed, 320 insertions(+), 27 deletions(-) diff --git a/DatabasetoEntityclass/App.config b/DatabasetoEntityclass/App.config index 56efbc7..afe1db2 100644 --- a/DatabasetoEntityclass/App.config +++ b/DatabasetoEntityclass/App.config @@ -1,6 +1,18 @@ - + + + + + + + + + + + + + \ No newline at end of file diff --git a/DatabasetoEntityclass/DatabasetoEntityclass.csproj b/DatabasetoEntityclass/DatabasetoEntityclass.csproj index a0425dd..c2fb37d 100644 --- a/DatabasetoEntityclass/DatabasetoEntityclass.csproj +++ b/DatabasetoEntityclass/DatabasetoEntityclass.csproj @@ -33,11 +33,48 @@ 4 - - D:\mes_db\NCA_MES_Models\CommonLibary\MySql.Data.dll + + ..\packages\Portable.BouncyCastle.1.9.0\lib\net40\BouncyCastle.Crypto.dll + + + ..\packages\Google.Protobuf.3.21.9\lib\net45\Google.Protobuf.dll + + + ..\packages\K4os.Compression.LZ4.1.3.5\lib\net462\K4os.Compression.LZ4.dll + + + ..\packages\K4os.Compression.LZ4.Streams.1.3.5\lib\net462\K4os.Compression.LZ4.Streams.dll + + + ..\packages\K4os.Hash.xxHash.1.0.8\lib\net462\K4os.Hash.xxHash.dll + + + ..\packages\MySql.Data.8.0.33\lib\net462\MySql.Data.dll + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + + ..\packages\System.IO.Pipelines.5.0.2\lib\net461\System.IO.Pipelines.dll + + + + ..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + @@ -71,6 +108,7 @@ True Resources.resx + SettingsSingleFileGenerator Settings.Designer.cs @@ -85,7 +123,9 @@ - + + PreserveNewest + \ No newline at end of file diff --git a/DatabasetoEntityclass/DbHelper.cs b/DatabasetoEntityclass/DbHelper.cs index 267be8d..11d5207 100644 --- a/DatabasetoEntityclass/DbHelper.cs +++ b/DatabasetoEntityclass/DbHelper.cs @@ -25,7 +25,8 @@ namespace WindowsFormsApp1 /// 获取连接字符串 /// - public static string connectionString = "server=10.164.1.169;port=3306;user=root;password=3687988; database=mesdb_nca;";//GetConnectionStringFromXml("./SystemConfig.xml"); + public static string connectionString = GetConnectionStringFromXml("./SystemConfig.xml"); + //"server=10.164.1.169;port=3306;user=root;password=3687988; database=mesdb_nca;";//GetConnectionStringFromXml("./SystemConfig.xml"); public static string DbName = getDbName();//"mesdb_nca"; @@ -43,16 +44,6 @@ namespace WindowsFormsApp1 return ""; } - private MySqlConnection connection; - - /// - /// DbHelperMySQL无参数构造函数 - /// - private DbHelperMySQL(MySqlConnection connection) - { - this.connection = connection; - } - public static string GetConnectionStringFromXml(string xmlFilePath) { try @@ -74,6 +65,17 @@ namespace WindowsFormsApp1 } } + private MySqlConnection connection; + + /// + /// DbHelperMySQL无参数构造函数 + /// + private DbHelperMySQL(MySqlConnection connection) + { + this.connection = connection; + } + + #region 公用方法 /// /// 获取表中最大的Id值 diff --git a/DatabasetoEntityclass/Form1.Designer.cs b/DatabasetoEntityclass/Form1.Designer.cs index f9715e9..7e35026 100644 --- a/DatabasetoEntityclass/Form1.Designer.cs +++ b/DatabasetoEntityclass/Form1.Designer.cs @@ -35,6 +35,13 @@ this.namespace_name = new System.Windows.Forms.TextBox(); this.lable2 = new System.Windows.Forms.Label(); this.txt_tablename = new System.Windows.Forms.ComboBox(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.button2 = new System.Windows.Forms.Button(); + this.label4 = new System.Windows.Forms.Label(); + this.button3 = new System.Windows.Forms.Button(); + this.button4 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 @@ -50,7 +57,7 @@ // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(70, 120); + this.label2.Location = new System.Drawing.Point(296, 16); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 4; @@ -76,7 +83,7 @@ // // namespace_name // - this.namespace_name.Location = new System.Drawing.Point(117, 155); + this.namespace_name.Location = new System.Drawing.Point(131, 86); this.namespace_name.Name = "namespace_name"; this.namespace_name.Size = new System.Drawing.Size(218, 21); this.namespace_name.TabIndex = 70; @@ -85,7 +92,7 @@ // lable2 // this.lable2.AutoSize = true; - this.lable2.Location = new System.Drawing.Point(53, 160); + this.lable2.Location = new System.Drawing.Point(67, 91); this.lable2.Name = "lable2"; this.lable2.Size = new System.Drawing.Size(59, 12); this.lable2.TabIndex = 71; @@ -94,18 +101,91 @@ // txt_tablename // this.txt_tablename.FormattingEnabled = true; - this.txt_tablename.Location = new System.Drawing.Point(117, 116); + this.txt_tablename.Location = new System.Drawing.Point(343, 12); this.txt_tablename.Name = "txt_tablename"; this.txt_tablename.Size = new System.Drawing.Size(218, 20); this.txt_tablename.TabIndex = 72; this.txt_tablename.Text = "请选择"; this.txt_tablename.TextUpdate += new System.EventHandler(this.orderId_TextUpdate); // + // textBox1 + // + this.textBox1.Font = new System.Drawing.Font("宋体", 11F); + this.textBox1.Location = new System.Drawing.Point(455, 425); + this.textBox1.Multiline = true; + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(335, 341); + this.textBox1.TabIndex = 73; + // + // textBox2 + // + this.textBox2.Location = new System.Drawing.Point(142, 469); + this.textBox2.Name = "textBox2"; + this.textBox2.Size = new System.Drawing.Size(218, 21); + this.textBox2.TabIndex = 74; + this.textBox2.Text = "uiDataGridView1"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(70, 472); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(65, 12); + this.label1.TabIndex = 75; + this.label1.Text = "控件名称:"; + // + // button2 + // + this.button2.Location = new System.Drawing.Point(69, 630); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(75, 23); + this.button2.TabIndex = 76; + this.button2.Text = "生成1"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(25, 405); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(827, 12); + this.label4.TabIndex = 77; + this.label4.Text = "-------------------------------------------------------------DataGridView生成------" + + "------------------------------------------------------"; + // + // button3 + // + this.button3.Location = new System.Drawing.Point(176, 630); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(75, 23); + this.button3.TabIndex = 78; + this.button3.Text = "生成2"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); + // + // button4 + // + this.button4.Location = new System.Drawing.Point(285, 630); + this.button4.Name = "button4"; + this.button4.Size = new System.Drawing.Size(75, 23); + this.button4.TabIndex = 79; + this.button4.Text = "生成3"; + this.button4.UseVisualStyleBackColor = true; + this.button4.Click += new System.EventHandler(this.button4_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(859, 778); + this.Controls.Add(this.button4); + this.Controls.Add(this.button3); + this.Controls.Add(this.label4); + this.Controls.Add(this.button2); + this.Controls.Add(this.label1); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.textBox1); this.Controls.Add(this.txt_tablename); this.Controls.Add(this.lable2); this.Controls.Add(this.namespace_name); @@ -130,6 +210,13 @@ private System.Windows.Forms.TextBox namespace_name; private System.Windows.Forms.Label lable2; private System.Windows.Forms.ComboBox txt_tablename; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Button button3; + private System.Windows.Forms.Button button4; } } diff --git a/DatabasetoEntityclass/Form1.cs b/DatabasetoEntityclass/Form1.cs index 7d6afae..e81fc2e 100644 --- a/DatabasetoEntityclass/Form1.cs +++ b/DatabasetoEntityclass/Form1.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace WindowsFormsApp1 { @@ -103,6 +104,160 @@ namespace WindowsFormsApp1 } + private void button2_Click(object sender, EventArgs e) + { + string tbname = txt_tablename.Text.Trim();//要生成的表名 + if (tbname.Equals("请选择") || tbname.Equals("")) + { + MessageBox.Show("请选择表"); + return; + }; + var dt = GetDbTableInfo(tbname); + string uidatagridview = textBox2.Text.Trim(); + textBox1.Text = ""; + textBox1.Text += $"DataTable data = DbHelperMySQL.Query(\"select * from {tbname}\");\n"; + textBox1.Text += "this."+ uidatagridview + ".DataSource = new BindingSource {DataSource = data};\n"; + for (int i = 0; i < dt.Rows.Count; i++) + { + var Name = dt.Rows[i]["Name"].ToString(); + textBox1.Text += $"var {Name}Column = {uidatagridview}.Columns[\"{Name}\"];\n"; + } + + for (int i = 0; i < dt.Rows.Count; i++) + { + var Name = dt.Rows[i]["Name"].ToString(); + var IsNullable = dt.Rows[i]["COMMENT"].ToString(); + textBox1.Text += $"{Name}Column.HeaderText = \"{IsNullable}\";\n"; + } + + textBox1.Text += $" {uidatagridview}.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;\n"; + textBox1.Text += $" {uidatagridview}.AutoGenerateColumns = true;\n"; + // textBox1.Text += $" {uidatagridview}.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;\n"; + } + + private void button3_Click(object sender, EventArgs e) + { + string tbname = txt_tablename.Text.Trim();//要生成的表名 + if (tbname.Equals("请选择") || tbname.Equals("")) + { + MessageBox.Show("请选择表"); + return; + }; + var dt = GetDbTableInfo(tbname); + string uidatagridview = textBox2.Text.Trim(); + textBox1.Text = ""; + textBox1.Text += $" string sql = $\"SELECT column_name, column_comment from information_schema.COLUMNS where table_name = '{tbname}' ORDER BY ORDINAL_POSITION; \";\r\n" + + $" DataTable dt = DbHelperMySQL.Query(sql);\n"; + textBox1.Text += "List cols = new List();\n"; + textBox1.Text += "for (int i = 0; i < dt.Rows.Count; i++){\n" + + " DataGridViewTextBoxColumn temp = new DataGridViewTextBoxColumn();\n" + + "temp.Name = dt.Rows[i][0].ToString();\n" + + "temp.HeaderText = dt.Rows[i][1].ToString();\n" + + "cols.Add(temp);" + + "}\n"; + textBox1.Text += "DataGridViewButtonColumn editBtn = new DataGridViewButtonColumn();\r\n" + + "editBtn.HeaderText = \"编辑\";\r\n" + + "editBtn.Name = \"edit\";\r\n" + + "editBtn.UseColumnTextForButtonValue = true;\r\n" + + "editBtn.Frozen = true;\n"; + textBox1.Text += " DataGridViewButtonColumn addBtn = new DataGridViewButtonColumn();\r\n" + + "addBtn.HeaderText = \"修改\";\r\n" + + "addBtn.Name = \"editgd\";\r\n" + + "addBtn.Text = \"editgd\";\r\n" + + "addBtn.UseColumnTextForButtonValue = true;\r\n" + + "addBtn.Frozen = true;\n "; + textBox1.Text += "cols.Add(editBtn);\n cols.Add(addBtn);\n"; + textBox1.Text += "if (cols.Last().Frozen)\r\n" + + "{\r\n " + + $"this.{uidatagridview}.RightToLeft = RightToLeft.Yes;\r\n" + + "cols.Reverse();\r\n" + + "}\r\n" + + $"this.{uidatagridview}.Columns.AddRange(cols.ToArray());\r\n\r\n" + + $"if (this.{uidatagridview}.Columns.Count > 0)\r\n" + + $"this.{uidatagridview}.FirstDisplayedScrollingColumnIndex = this.{uidatagridview}.Columns.Count - 2;\n"; + textBox1.Text += $" {uidatagridview}.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;\n"; + textBox1.Text += $"DataTable data = DbHelperMySQL.Query(\"select * from {tbname}\");\n"; + textBox1.Text += $"this.{uidatagridview}.Rows.AddRange(data.AsEnumerable().Select(row => {{\r\n" + + "DataGridViewRow dataGridViewRow = new DataGridViewRow();\r\n " + + "DataGridViewButtonCell addCell = new DataGridViewButtonCell();\r\n" + + "addCell.Value = \"新增\";\r\n " + + "dataGridViewRow.Cells.Add(addCell);\r\n " + + "DataGridViewButtonCell editCell = new DataGridViewButtonCell();\r\n" + + "editCell.Value = \"修改\";\r\n " + + "dataGridViewRow.Cells.Add(editCell);\r\n " + + "foreach (var item in row.ItemArray.Reverse())\r\n " + + "{\r\n" + + "DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();\r\n" + + "cell.Value = item;\r\n " + + "dataGridViewRow.Cells.Add(cell);\r\n" + + "}\r\n" + + "dataGridViewRow.Height = 40;\r\n" + + "return dataGridViewRow;\r\n" + + "}).ToArray());"; + + } + private void button4_Click(object sender, EventArgs e) + { + string tbname = txt_tablename.Text.Trim();//要生成的表名 + if (tbname.Equals("请选择") || tbname.Equals("")) + { + MessageBox.Show("请选择表"); + return; + }; + var dt = GetDbTableInfo(tbname); + string uidatagridview = textBox2.Text.Trim(); + textBox1.Text = ""; + textBox1.Text += "List cols = new List();\n"; + for (int i = 0; i < dt.Rows.Count; i++) + { + var Name = dt.Rows[i]["Name"].ToString(); + var IsNullable = dt.Rows[i]["COMMENT"].ToString(); + textBox1.Text += $"DataGridViewTextBoxColumn {Name}Cloumn = new DataGridViewTextBoxColumn();\n"; + textBox1.Text += $"{Name}Cloumn.Name = \"{Name}\";" + + $"{Name}Cloumn.HeaderText =\"{IsNullable}\";" + + $"cols.Add({Name}Cloumn);\n"; + } + textBox1.Text += "DataGridViewButtonColumn editBtn = new DataGridViewButtonColumn();\r\n" + + "editBtn.HeaderText = \"编辑\";\r\n" + + "editBtn.Name = \"edit\";\r\n" + + "editBtn.UseColumnTextForButtonValue = true;\r\n" + + "editBtn.Frozen = true;\n"; + textBox1.Text += " DataGridViewButtonColumn addBtn = new DataGridViewButtonColumn();\r\n" + + "addBtn.HeaderText = \"修改\";\r\n" + + "addBtn.Name = \"editgd\";\r\n" + + "addBtn.Text = \"editgd\";\r\n" + + "addBtn.UseColumnTextForButtonValue = true;\r\n" + + "addBtn.Frozen = true;\n "; + textBox1.Text += "cols.Add(editBtn);\n cols.Add(addBtn);\n"; + textBox1.Text += "if (cols.Last().Frozen)\r\n" + + "{\r\n " + + $"this.{uidatagridview}.RightToLeft = RightToLeft.Yes;\r\n" + + "cols.Reverse();\r\n" + + "}\r\n" + + $"this.{uidatagridview}.Columns.AddRange(cols.ToArray());\r\n\r\n" + + $"if (this.{uidatagridview}.Columns.Count > 0)\r\n" + + $"this.{uidatagridview}.FirstDisplayedScrollingColumnIndex = this.{uidatagridview}.Columns.Count - 2;\n"; + textBox1.Text += $" {uidatagridview}.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;\n"; + textBox1.Text += $"DataTable data = DbHelperMySQL.Query(\"select * from {tbname}\");\n"; + textBox1.Text += $"this.{uidatagridview}.Rows.AddRange(data.AsEnumerable().Select(row => {{\r\n" + + "DataGridViewRow dataGridViewRow = new DataGridViewRow();\r\n " + + "DataGridViewButtonCell addCell = new DataGridViewButtonCell();\r\n" + + "addCell.Value = \"新增\";\r\n " + + "dataGridViewRow.Cells.Add(addCell);\r\n " + + "DataGridViewButtonCell editCell = new DataGridViewButtonCell();\r\n" + + "editCell.Value = \"修改\";\r\n " + + "dataGridViewRow.Cells.Add(editCell);\r\n " + + "foreach (var item in row.ItemArray.Reverse())\r\n " + + "{\r\n" + + "DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();\r\n" + + "cell.Value = item;\r\n " + + "dataGridViewRow.Cells.Add(cell);\r\n" + + "}\r\n" + + "dataGridViewRow.Height = 40;\r\n" + + "return dataGridViewRow;\r\n" + + "}).ToArray());"; + + } //订单下拉模糊搜索 private void orderId_TextUpdate(object sender, EventArgs e) //不要用TextChanged { @@ -124,10 +279,7 @@ namespace WindowsFormsApp1 /// public static DataTable GetDbTableInfo(string tabname) { - string str = string.Format(@"SELECT COLUMN_NAME AS Name, COLUMN_TYPE AS Type,COLUMN_COMMENT AS COMMENT -FROM INFORMATION_SCHEMA.COLUMNS -WHERE TABLE_NAME = '{0}';", tabname); - + string str = $"SELECT COLUMN_NAME AS Name, COLUMN_TYPE AS Type,COLUMN_COMMENT AS COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{tabname}' ORDER BY ORDINAL_POSITION;"; var dt = DbHelperMySQL.Query(str); return dt; } @@ -135,7 +287,7 @@ WHERE TABLE_NAME = '{0}';", tabname); public static List GetTableName(string tableName) { - string sql = $"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME LIKE '{tableName}%' limit 10;"; + string sql = $"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME LIKE '{tableName}%' limit 20;"; var dt = DbHelperMySQL.Query(sql); //查询数据库表中所有已录入人员 List sList = new List(); //存放数据库查询结果 diff --git a/DatabasetoEntityclass/SystemConfig.xml b/DatabasetoEntityclass/SystemConfig.xml index 54b03cd..4aecece 100644 --- a/DatabasetoEntityclass/SystemConfig.xml +++ b/DatabasetoEntityclass/SystemConfig.xml @@ -1,6 +1,6 @@  - - + +