diff --git a/DatabasetoEntityclass/Form1.Designer.cs b/DatabasetoEntityclass/Form1.Designer.cs index 7e35026..bcc6d58 100644 --- a/DatabasetoEntityclass/Form1.Designer.cs +++ b/DatabasetoEntityclass/Form1.Designer.cs @@ -42,6 +42,8 @@ this.label4 = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); + this.textBox3 = new System.Windows.Forms.TextBox(); + this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 @@ -174,11 +176,30 @@ this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // + // textBox3 + // + this.textBox3.Location = new System.Drawing.Point(858, 344); + this.textBox3.Multiline = true; + this.textBox3.Name = "textBox3"; + this.textBox3.Size = new System.Drawing.Size(98, 131); + this.textBox3.TabIndex = 80; + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(840, 322); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(137, 12); + this.label5.TabIndex = 81; + this.label5.Text = "查询字段sql自定义,可空"; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(859, 778); + this.ClientSize = new System.Drawing.Size(1084, 778); + this.Controls.Add(this.label5); + this.Controls.Add(this.textBox3); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.label4); @@ -217,6 +238,8 @@ private System.Windows.Forms.Label label4; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; + private System.Windows.Forms.TextBox textBox3; + private System.Windows.Forms.Label label5; } } diff --git a/DatabasetoEntityclass/Form1.cs b/DatabasetoEntityclass/Form1.cs index e81fc2e..48fc7b3 100644 --- a/DatabasetoEntityclass/Form1.cs +++ b/DatabasetoEntityclass/Form1.cs @@ -66,7 +66,15 @@ namespace WindowsFormsApp1 return; }; string namespaceName = namespace_name.Text.Trim(); - var dt = GetDbTableInfo(tbname); + DataTable dt = null; + if (textBox3.Text.Trim() == "") + { + dt = GetDbTableInfo(tbname); + } + else + { + dt = DbHelperMySQL.Query(textBox3.Text.Trim()); + } var list = new List(); txt_ret.Text += "using System;\n" + "using NCA_MES_Models.CommonUtils.DB.DBAttribute;\n\n"; @@ -112,7 +120,15 @@ namespace WindowsFormsApp1 MessageBox.Show("请选择表"); return; }; - var dt = GetDbTableInfo(tbname); + DataTable dt = null; + if (textBox3.Text.Trim() == "") + { + dt = GetDbTableInfo(tbname); + } + else + { + dt = DbHelperMySQL.Query(textBox3.Text.Trim()); + } string uidatagridview = textBox2.Text.Trim(); textBox1.Text = ""; textBox1.Text += $"DataTable data = DbHelperMySQL.Query(\"select * from {tbname}\");\n"; @@ -143,9 +159,17 @@ namespace WindowsFormsApp1 MessageBox.Show("请选择表"); return; }; - var dt = GetDbTableInfo(tbname); + DataTable dt = null; + if (textBox3.Text.Trim() == "") + { + dt = GetDbTableInfo(tbname); + } + else { + dt = DbHelperMySQL.Query(textBox3.Text.Trim()); + } + string uidatagridview = textBox2.Text.Trim(); - textBox1.Text = ""; + textBox1.Text = $"this.{uidatagridview}.AutoGenerateColumns = false;"; 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"; @@ -153,17 +177,19 @@ namespace WindowsFormsApp1 " DataGridViewTextBoxColumn temp = new DataGridViewTextBoxColumn();\n" + "temp.Name = dt.Rows[i][0].ToString();\n" + "temp.HeaderText = dt.Rows[i][1].ToString();\n" + + "temp.DataPropertyName= dt.Rows[i][0].ToString();\n" + "cols.Add(temp);" + "}\n"; textBox1.Text += "DataGridViewButtonColumn editBtn = new DataGridViewButtonColumn();\r\n" + "editBtn.HeaderText = \"编辑\";\r\n" + - "editBtn.Name = \"edit\";\r\n" + + "editBtn.Name = \"editBtn\";\r\n" + + "editBtn.Text = \"编辑\";\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.HeaderText = \"新增\";\r\n" + + "addBtn.Name = \"addBtn\";\r\n" + + "addBtn.Text = \"新增\";\r\n" + "addBtn.UseColumnTextForButtonValue = true;\r\n" + "addBtn.Frozen = true;\n "; textBox1.Text += "cols.Add(editBtn);\n cols.Add(addBtn);\n"; @@ -177,23 +203,7 @@ namespace WindowsFormsApp1 $"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());"; + textBox1.Text += $"this.{uidatagridview}.DataSource = data;\n"; } private void button4_Click(object sender, EventArgs e) @@ -204,9 +214,18 @@ namespace WindowsFormsApp1 MessageBox.Show("请选择表"); return; }; - var dt = GetDbTableInfo(tbname); + + + DataTable dt = null; + if (textBox3.Text.Trim() == "") + { + dt = GetDbTableInfo(tbname); + } + else { + dt = DbHelperMySQL.Query(textBox3.Text.Trim()); + } string uidatagridview = textBox2.Text.Trim(); - textBox1.Text = ""; + textBox1.Text = $"this.{uidatagridview}.AutoGenerateColumns = false;"; textBox1.Text += "List cols = new List();\n"; for (int i = 0; i < dt.Rows.Count; i++) { @@ -215,19 +234,21 @@ namespace WindowsFormsApp1 textBox1.Text += $"DataGridViewTextBoxColumn {Name}Cloumn = new DataGridViewTextBoxColumn();\n"; textBox1.Text += $"{Name}Cloumn.Name = \"{Name}\";" + $"{Name}Cloumn.HeaderText =\"{IsNullable}\";" + + $"{Name}Cloumn.DataPropertyName = \"{Name}\";" + $"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"; + "editBtn.HeaderText = \"编辑\";\r\n" + + "editBtn.Name = \"editBtn\";\r\n" + + "editBtn.Text = \"编辑\";\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 "; + "addBtn.HeaderText = \"新增\";\r\n" + + "addBtn.Name = \"addBtn\";\r\n" + + "addBtn.Text = \"新增\";\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 " + @@ -239,23 +260,24 @@ namespace WindowsFormsApp1 $"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());"; + textBox1.Text += $"this.{uidatagridview}.DataSource = data;\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());";*/ } //订单下拉模糊搜索 @@ -279,6 +301,7 @@ namespace WindowsFormsApp1 /// public static DataTable GetDbTableInfo(string 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;