@ -11,6 +11,7 @@ using System.Linq;
using System.Text ;
using System.Text ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using System.Windows.Forms ;
using static System . Windows . Forms . VisualStyles . VisualStyleElement ;
namespace WindowsFormsApp1
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<DataGridViewColumn> cols = new List<DataGridViewColumn>();\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<DataGridViewColumn> cols = new List<DataGridViewColumn>();\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
private void orderId_TextUpdate ( object sender , EventArgs e ) //不要用TextChanged
{
{
@ -124,10 +279,7 @@ namespace WindowsFormsApp1
/// <param name="tabname"></param>
/// <param name="tabname"></param>
public static DataTable GetDbTableInfo ( string tabname )
public static DataTable GetDbTableInfo ( string tabname )
{
{
string str = string . Format ( @ "SELECT COLUMN_NAME AS Name, COLUMN_TYPE AS Type,COLUMN_COMMENT AS COMMENT
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;" ;
FROM INFORMATION_SCHEMA . COLUMNS
WHERE TABLE_NAME = ' { 0 } ' ; ", tabname);
var dt = DbHelperMySQL . Query ( str ) ;
var dt = DbHelperMySQL . Query ( str ) ;
return dt ;
return dt ;
}
}
@ -135,7 +287,7 @@ WHERE TABLE_NAME = '{0}';", tabname);
public static List < string > GetTableName ( string tableName )
public static List < string > GetTableName ( string tableName )
{
{
string sql = $"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME LIKE '{tableName}%' limit 1 0;";
string sql = $"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME LIKE '{tableName}%' limit 2 0;";
var dt = DbHelperMySQL . Query ( sql ) ;
var dt = DbHelperMySQL . Query ( sql ) ;
//查询数据库表中所有已录入人员
//查询数据库表中所有已录入人员
List < string > sList = new List < string > ( ) ; //存放数据库查询结果
List < string > sList = new List < string > ( ) ; //存放数据库查询结果