@ -1,19 +1,11 @@
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Data.SqlTypes ;
using System.Drawing ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using static System . Windows . Forms . VisualStyles . VisualStyleElement ;
using GenerateClass.Util ;
using JinianNet.JNTemplate ;
namespace WindowsFormsApp1
namespace GenerateClass
{
public partial class Form1 : Form
{
@ -21,402 +13,121 @@ namespace WindowsFormsApp1
{
InitializeComponent ( ) ;
}
string folderName = "GenFolder" ; // 文件夹名称
private void Form1_Load ( object sender , EventArgs e )
{
this . txt_tablename . Items . AddRange ( GetTableName ( ) . ToArray ( ) ) ;
this . txt_tablename . Items . AddRange ( DbUtil . GetTableName ( ) . ToArray ( ) ) ;
this . txt_tablename . TextUpdate + = ( childSender , childE ) = > {
string str = this . txt_tablename . Text ; //获取输入内容
List < string > sList = DbUtil . GetTableName ( str ) ; //存放数据库查询结果
//提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容)
this . txt_tablename . DroppedDown = true ; //显示下拉列表,但是显示后鼠标指针就不见了
Cursor . Current = Cursors . Default ; //将指针显示出来
//在表中已录入名字中寻找包含输入内容的项 有则添加到comboBox项中
this . txt_tablename . Items . Clear ( ) ;
this . txt_tablename . Items . AddRange ( sList . ToArray ( ) ) ;
this . txt_tablename . Select ( this . txt_tablename . Text . Length , 0 ) ;
} ;
}
/// <summary>
/// 将数据库字段转换为驼峰命名
/// </summary>
/// <param name="dbField"></param>
/// <returns></returns>
public static string ToCamelCase ( string dbField )
private void modelGen_Click ( object sender , EventArgs e )
{
string [ ] words = dbField . Split ( '_' ) ;
for ( int i = 1 ; i < words . Length ; i + + )
{
words [ i ] = char . ToUpper ( words [ i ] [ 0 ] ) + words [ i ] . Substring ( 1 ) ;
}
return string . Join ( "" , words ) ;
fillModelTemplate ( ) ;
}
// 根据数据库表名生成实体类名称
static string GenerateClassName ( string tableName )
private void dgvGen1_Click ( object sender , EventArgs e )
{
string [ ] words = tableName . Split ( '_' ) ;
StringBuilder sb = new StringBuilder ( ) ;
foreach ( string word in words )
{
sb . Append ( CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( word . ToLower ( ) ) ) ;
}
return sb . ToString ( ) ;
fillDgv1Template ( ) ;
}
private void button1_Click ( object sender , EventArgs e )
private void dgvGen2_Click ( object sender , EventArgs e )
{
//string constr = txt_constr.Text.Trim();//数据库链接字符串
txt_ret . Text = "" ;
string tbname = txt_tablename . Text . Trim ( ) ; //要生成的表名
if ( tbname . Equals ( "请选择" ) | | tbname . Equals ( "" ) ) {
MessageBox . Show ( "请选择表" ) ;
return ;
} ;
string namespaceName = namespace_name . Text . Trim ( ) ;
DataTable dt = null ;
if ( textBox3 . Text . Trim ( ) = = "" )
{
dt = GetDbTableInfo ( tbname ) ;
}
else
{
dt = DbHelperMySQL . Query ( textBox3 . Text . Trim ( ) ) ;
}
var list = new List < string > ( ) ;
txt_ret . Text + = "using System;\n" +
"using NCA_MES_Models.CommonUtils.DB.DBAttribute;\n\n" ;
txt_ret . Text + = $"namespace {namespaceName} {{\n\n" ;
txt_ret . Text + = "/// <summary>" + "\n" +
$"/// {GetTableComment(tbname)}\n" +
"/// </summary>" + "\n" +
$"[TableName(\" { tbname } \ ")]\n" +
$"public class {GenerateClassName(tbname)}: DBBaseModel<{GenerateClassName(tbname)}>{{" ;
for ( int i = 0 ; i < dt . Rows . Count ; i + + )
{
var Name = dt . Rows [ i ] [ "Name" ] . ToString ( ) ;
var Type = dt . Rows [ i ] [ "Type" ] . ToString ( ) ;
var IsNullable = dt . Rows [ i ] [ "COMMENT" ] . ToString ( ) ;
var KeyType = dt . Rows [ i ] [ "KeyType" ] . ToString ( ) ;
Type type = DbTypeStr_To_CsharpType ( Type ) ;
txt_ret . Text + = "\n\n\t" + "/// <summary>" +
"\n\t" + $"///{IsNullable}" +
"\n\t" + "/// </summary>" +
( String . IsNullOrEmpty ( KeyType ) ? "" : "\n\t[TableId]" ) +
"\n\t" + $"[TableField(\" { Name } \ ",\"{IsNullable}\")]" +
"\n\t" + $"public {type.Name} {ToCamelCase(Name)} {{ get; set; }}" ;
//加注释记得引用using
}
txt_ret . Text + = "\n}\n}" ;
// 创建文件夹
string folderPath = Path . Combine ( Environment . CurrentDirectory , folderName ) ;
Directory . CreateDirectory ( folderPath ) ;
// 创建文件并写入内容
string filePath = Path . Combine ( folderPath , GenerateClassName ( tbname ) + ".cs" ) ;
File . WriteAllText ( filePath , txt_ret . Text ) ;
// 打开文件夹
System . Diagnostics . Process . Start ( folderPath ) ;
fillDgv2Template ( ) ;
}
private void button2_Click ( object sender , EventArgs e )
private void dgvGen3_Click ( object sender , EventArgs e )
{
string tbname = txt_tablename . Text . Trim ( ) ; //要生成的表名
if ( tbname . Equals ( "请选择" ) | | tbname . Equals ( "" ) )
{
MessageBox . Show ( "请选择表" ) ;
return ;
} ;
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" ;
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";
fillDgv3Template ( ) ;
}
private void button3_Click ( object sender , EventArgs e )
private void fillModelTemplate ( )
{
string tbname = txt_tablename . Text . Trim ( ) ; //要生成的表名
if ( tbname . Equals ( "请选择" ) | | tbname . Equals ( "" ) )
{
MessageBox . Show ( "请选择表" ) ;
return ;
} ;
DataTable dt = null ;
if ( textBox3 . Text . Trim ( ) = = "" )
{
dt = GetDbTableInfo ( tbname ) ;
}
else {
dt = DbHelperMySQL . Query ( textBox3 . Text . Trim ( ) ) ;
string namespaceName = namespace_name . Text . Trim ( ) ; //名称空间名称
List < Dictionary < string , string > > filedInfos = DbUtil . getTableInfo ( tbname , textBox3 . Text . Trim ( ) ) ;
using ( StreamReader reader = new StreamReader ( @".\Template\ModelFile.template" ) )
{
string fileContent = reader . ReadToEnd ( ) ;
var template = Engine . CreateTemplate ( fileContent ) ;
template . Set ( "namespace_name" , namespaceName ) ;
template . Set ( "table_comment" , DbUtil . GetTableComment ( tbname ) ) ;
template . Set ( "sql_table_name" , tbname ) ;
template . Set ( "table_name" , DbUtil . GenerateClassName ( tbname ) ) ;
template . Set ( "filedInfos" , filedInfos ) ;
template . SetStaticType ( "string" , typeof ( string ) ) ;
var result = template . Render ( ) ;
this . txt_ret . Text = result ;
DbUtil . openFile ( tbname , result ) ;
}
string uidatagridview = textBox2 . Text . Trim ( ) ;
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<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" +
"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 = \"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 = \"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 " +
$"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}.DataSource = data;\n" ;
}
private void button4_Click( object sender , EventArgs e )
private void fillDgv1Template ( )
{
string tbname = txt_tablename . Text . Trim ( ) ; //要生成的表名
if ( tbname . Equals ( "请选择" ) | | tbname . Equals ( "" ) )
{
MessageBox . Show ( "请选择表" ) ;
return ;
} ;
DataTable dt = null ;
if ( textBox3 . Text . Trim ( ) = = "" )
{
dt = GetDbTableInfo ( tbname ) ;
}
else {
dt = DbHelperMySQL . Query ( textBox3 . Text . Trim ( ) ) ;
}
string uidatagridview = textBox2 . Text . Trim ( ) ;
textBox1 . Text = $"this.{uidatagridview}.AutoGenerateColumns = false;" ;
textBox1 . Text + = "List<DataGridViewColumn> cols = new List<DataGridViewColumn>();\n\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 } \ ";" +
$"{Name}Cloumn.DataPropertyName = \" { Name } \ ";" +
$"cols.Add({Name}Cloumn);\n\n" ;
}
textBox1 . Text + = "DataGridViewButtonColumn editBtn = new DataGridViewButtonColumn();\r\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 = \"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 " +
$"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}.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());" ; * /
}
//订单下拉模糊搜索
private void orderId_TextUpdate ( object sender , EventArgs e ) //不要用TextChanged
{
string s = this . txt_tablename . Text ; //获取输入内容
List < string > sList = GetTableName ( s ) ; //存放数据库查询结果
//提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容)
this . txt_tablename . DroppedDown = true ; //显示下拉列表,但是显示后鼠标指针就不见了
Cursor . Current = Cursors . Default ; //将指针显示出来
//在表中已录入名字中寻找包含输入内容的项 有则添加到comboBox项中
this . txt_tablename . Items . Clear ( ) ;
this . txt_tablename . Items . AddRange ( sList . ToArray ( ) ) ;
this . txt_tablename . Select ( this . txt_tablename . Text . Length , 0 ) ;
}
/// <summary>
/// 根据参数,获取数据表信息
/// </summary>
/// <param name="tabname"></param>
public static DataTable GetDbTableInfo ( string tabname )
{
string sql = @ "SELECT
c . COLUMN_NAME AS Name ,
c . COLUMN_TYPE AS Type ,
c . COLUMN_COMMENT AS Comment ,
CASE WHEN CONSTRAINT_NAME = ' PRIMARY ' THEN ' PRIMARY KEY ' ELSE ' ' END AS KeyType
FROM
INFORMATION_SCHEMA . COLUMNS c
LEFT JOIN
INFORMATION_SCHEMA . KEY_COLUMN_USAGE k
ON
c . TABLE_SCHEMA = k . TABLE_SCHEMA
AND c . TABLE_NAME = k . TABLE_NAME
AND c . COLUMN_NAME = k . COLUMN_NAME
WHERE
c . TABLE_NAME = ' { 0 } '
ORDER BY
c . ORDINAL_POSITION ; ";
//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 ( String . Format ( sql , tabname ) ) ;
return dt ;
}
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 20;" ;
var dt = DbHelperMySQL . Query ( sql ) ;
//查询数据库表中所有已录入人员
List < string > sList = new List < string > ( ) ; //存放数据库查询结果
for ( int i = 0 ; i < dt . Rows . Count ; i + + )
{
sList . Add ( dt . Rows [ i ] [ "TABLE_NAME" ] . ToString ( ) ) ;
string dgvName = textBox2 . Text . Trim ( ) ; //dgv空间名称
List < Dictionary < string , string > > filedInfos = DbUtil . getTableInfo ( tbname , textBox3 . Text . Trim ( ) ) ;
List < string > timeFileds = DbUtil . getTimeFields ( filedInfos ) ;
using ( StreamReader reader = new StreamReader ( @".\Template\DataGridView_1.template" ) )
{
string fileContent = reader . ReadToEnd ( ) ;
var template = Engine . CreateTemplate ( fileContent ) ;
template . Set ( "table_comment" , DbUtil . GetTableComment ( tbname ) ) ;
template . Set ( "sql_table_name" , tbname ) ;
template . Set ( "table_name" , DbUtil . GenerateClassName ( tbname ) ) ;
template . Set ( "dgv_name" , dgvName ) ;
template . Set ( "filedInfos" , filedInfos ) ;
template . Set ( "timeFiledStr" , DbUtil . joinList ( timeFileds ) ) ;
template . SetStaticType ( "string" , typeof ( string ) ) ;
var result = template . Render ( ) ;
this . textBox1 . Text = result ;
}
return sList ;
//return dt;
}
public static string GetTableComment ( string tableName )
{
string sql = $"SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME ='{tableName}';" ;
var dt = DbHelperMySQL . Query ( sql ) ;
if ( dt . Rows . Count < 1 ) return tableName ;
return dt . Rows [ 0 ] [ "TABLE_COMMENT" ] . ToString ( ) ;
}
public static List < string > GetTableName ( )
private void fillDgv2Template ( )
{
string sql = $"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}';" ;
var dt = DbHelperMySQL . Query ( sql ) ;
//查询数据库表中所有已录入人员
List < string > sList = new List < string > ( ) ; //存放数据库查询结果
for ( int i = 0 ; i < dt . Rows . Count ; i + + )
{
sList . Add ( dt . Rows [ i ] [ "TABLE_NAME" ] . ToString ( ) ) ;
string tbname = txt_tablename . Text . Trim ( ) ; //要生成的表名
string dgvName = textBox2 . Text . Trim ( ) ; //dgv空间名称
List < string > timeFileds = DbUtil . getTimeFields ( DbUtil . getTableInfo ( tbname , textBox3 . Text . Trim ( ) ) ) ;
using ( StreamReader reader = new StreamReader ( @".\Template\DataGridView_2.template" ) )
{
string fileContent = reader . ReadToEnd ( ) ;
var template = Engine . CreateTemplate ( fileContent ) ;
template . Set ( "table_comment" , DbUtil . GetTableComment ( tbname ) ) ;
template . Set ( "sql_table_name" , tbname ) ;
template . Set ( "table_name" , DbUtil . GenerateClassName ( tbname ) ) ;
template . Set ( "dgv_name" , dgvName ) ;
template . Set ( "isGenOperationColumn" , isGenOperationColumn . Checked ) ;
template . Set ( "timeFiledStr" , DbUtil . joinList ( timeFileds ) ) ;
template . SetStaticType ( "string" , typeof ( string ) ) ;
var result = template . Render ( ) ;
this . textBox1 . Text = result ;
}
return sList ;
//return dt;
}
/// <summary>
/// 类型转换枚举
/// </summary>
protected Dictionary < string , Type > DbTypeDic { get ; } = new Dictionary < string , Type > ( )
private void fillDgv3Template ( )
{
{ "int" , typeof ( Int32 ) } ,
{ "text" , typeof ( string ) } ,
{ "enum" , typeof ( string ) } ,
{ "bigint" , typeof ( Int64 ) } ,
{ "binary" , typeof ( byte [ ] ) } ,
{ "bit" , typeof ( bool ) } ,
{ "char" , typeof ( string ) } ,
{ "date" , typeof ( DateTime ) } ,
{ "datetime" , typeof ( DateTime ) } ,
{ "datetime2" , typeof ( DateTime ) } ,
{ "decimal" , typeof ( decimal ) } ,
{ "float" , typeof ( double ) } ,
{ "image" , typeof ( byte [ ] ) } ,
{ "money" , typeof ( decimal ) } ,
{ "nchar" , typeof ( string ) } ,
{ "ntext" , typeof ( string ) } ,
{ "numeric" , typeof ( decimal ) } ,
{ "nvarchar" , typeof ( string ) } ,
{ "real" , typeof ( Single ) } ,
{ "smalldatetime" , typeof ( DateTime ) } ,
{ "smallint" , typeof ( Int16 ) } ,
{ "smallmoney" , typeof ( decimal ) } ,
{ "timestamp" , typeof ( DateTime ) } ,
{ "tinyint" , typeof ( string ) } ,
{ "varbinary" , typeof ( byte [ ] ) } ,
{ "varchar" , typeof ( string ) } ,
{ "variant" , typeof ( object ) } ,
{ "uniqueidentifier" , typeof ( Guid ) } ,
} ;
/// <summary>
/// 获取字段类型
/// </summary>
/// <param name="dbTypeStr"></param>
/// <returns></returns>
public virtual Type DbTypeStr_To_CsharpType ( string dbTypeStr )
{
if ( dbTypeStr . Contains ( '(' ) )
{
string [ ] strings = dbTypeStr . Split ( '(' ) ;
dbTypeStr = strings [ 0 ] ;
string tbname = txt_tablename . Text . Trim ( ) ; //要生成的表名
string dgvName = textBox2 . Text . Trim ( ) ; //dgv空间名称
List < Dictionary < string , string > > filedInfos = DbUtil . getTableInfo ( tbname , textBox3 . Text . Trim ( ) ) ;
List < string > timeFileds = DbUtil . getTimeFields ( filedInfos ) ;
using ( StreamReader reader = new StreamReader ( @".\Template\DataGridView_3.template" ) )
{
string fileContent = reader . ReadToEnd ( ) ;
var template = Engine . CreateTemplate ( fileContent ) ;
template . Set ( "table_comment" , DbUtil . GetTableComment ( tbname ) ) ;
template . Set ( "sql_table_name" , tbname ) ;
template . Set ( "table_name" , DbUtil . GenerateClassName ( tbname ) ) ;
template . Set ( "dgv_name" , dgvName ) ;
template . Set ( "filedInfos" , filedInfos ) ;
template . Set ( "isGenOperationColumn" , isGenOperationColumn . Checked ) ;
template . Set ( "timeFiledStr" , DbUtil . joinList ( timeFileds ) ) ;
template . SetStaticType ( "string" , typeof ( string ) ) ;
var result = template . Render ( ) ;
this . textBox1 . Text = result ;
}
string _dbTypeStr = dbTypeStr . ToLower ( ) ;
Type type = null ;
if ( DbTypeDic . ContainsKey ( _dbTypeStr ) )
type = DbTypeDic [ _dbTypeStr ] ;
else
type = typeof ( string ) ;
return type ;
}
}
}