------------------------------------------------------------------
1)UI使用winform
2)Template Code 语言:C#
3)不好:不知道怎么通过input["tableNames"]取得界面上选中的表,我就简单处理了。不让你选了,就是全部的表了。
------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%
#REFERENCE System.Windows.Forms.dll, System.Drawing.dll
%>
<% #NAMESPACE System.Windows.Forms, System.Drawing %>
public class GeneratedGui : DotNetScriptGui
{
public GeneratedGui(ZeusContext context) : base (context) {}
// -----------------------------------------
// 入口函数
// -----------------------------------------
public override void Setup()
{
Form1 form = new Form1(MyMeta, input);
if (form.ShowDialog() != DialogResult.OK)
{
ui.IsCanceled = true ;
}
}
}
public class Form1:Form
{
private System.ComponentModel.IContainer components = null ;
protected override void Dispose( bool disposing)
{
if (disposing && (components != null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
private void InitializeComponent()
{
this .comboBox1 = new System.Windows.Forms.ComboBox();
this .listBox1 = new System.Windows.Forms.ListBox();
this .button1 = new System.Windows.Forms.Button();
this .SuspendLayout();
this .comboBox1.FormattingEnabled = true ;
this .comboBox1.Location = new System.Drawing.Point( 22 , 24 );
this .comboBox1.Name = " comboBox1 " ;
this .comboBox1.Size = new System.Drawing.Size( 233 , 20 );
this .comboBox1.TabIndex = 0 ;
this .comboBox1.SelectedIndexChanged += new System.EventHandler( this .comboBox1_SelectedIndexChanged);
this .comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this .listBox1.FormattingEnabled = true ;
this .listBox1.ItemHeight = 12 ;
this .listBox1.Location = new System.Drawing.Point( 22 , 50 );
this .listBox1.Name = " listBox1 " ;
this .listBox1.Size = new System.Drawing.Size( 233 , 196 );
this .listBox1.TabIndex = 1 ;
this .listBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
this .button1.Location = new System.Drawing.Point( 180 , 252 );
this .button1.Name = " button1 " ;
this .button1.Size = new System.Drawing.Size( 75 , 23 );
this .button1.TabIndex = 2 ;
this .button1.Text = " OK " ;
this .button1.UseVisualStyleBackColor = true ;
this .button1.Click += new System.EventHandler( this .button1_Click);
this .AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 284 , 293 );
this .Controls.Add( this .button1);
this .Controls.Add( this .listBox1);
this .Controls.Add( this .comboBox1);
this .MaximizeBox = false ;
this .MinimizeBox = false ;
this .Name = " Form1 " ;
this .Text = " BLL Class " ;
this .Load += new System.EventHandler( this .Form1_Load);
this .ResumeLayout( false );
}
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private dbRoot myMeta;
private IZeusInput zeusInput;
public Form1(dbRoot myMeta, IZeusInput zeusInput)
{
this .myMeta = myMeta;
this .zeusInput = zeusInput;
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
IDatabase database = this .comboBox1.SelectedValue as IDatabase;
if (database != null )
{
this .listBox1.DataSource = database.Tables;
this .listBox1.DisplayMember = " Name " ;
}
}
private void Form1_Load( object sender, EventArgs e)
{
// 获取数据库,传给ComboBox
comboBox1.DataSource = this .myMeta.Databases;
// ComboBox显示数据库名
this .comboBox1.DisplayMember = " Name " ;
if ( this .myMeta.DefaultDatabase != null )
{
// 选中默认的数据库
this .comboBox1.SelectedIndex = this .comboBox1.FindStringExact( this .myMeta.DefaultDatabase.Name);
// 通过IDatabase 的Tables属性取得数据库里面所有的数据表
// 作为数据源传给ListBox
this .listBox1.DataSource = this .myMeta.DefaultDatabase.Tables;
// ListBox显示数据表的名字
this .listBox1.DisplayMember = " Name " ;
}
}
private void button1_Click( object sender, EventArgs e)
{
IDatabase database = this .comboBox1.SelectedValue as IDatabase;
ListBox.SelectedObjectCollection tables = this .listBox1.SelectedItems ;
this .zeusInput[ " databaseNames " ] = database.Name;
this .zeusInput[ " tableNames " ] = tables; // 不用自己手动选择该处理哪些表 全部表!
this .DialogResult = DialogResult.OK;
this .Close();
}
}
<% #NAMESPACE System.Windows.Forms, System.Drawing %>
public class GeneratedGui : DotNetScriptGui
{
public GeneratedGui(ZeusContext context) : base (context) {}
// -----------------------------------------
// 入口函数
// -----------------------------------------
public override void Setup()
{
Form1 form = new Form1(MyMeta, input);
if (form.ShowDialog() != DialogResult.OK)
{
ui.IsCanceled = true ;
}
}
}
public class Form1:Form
{
private System.ComponentModel.IContainer components = null ;
protected override void Dispose( bool disposing)
{
if (disposing && (components != null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
private void InitializeComponent()
{
this .comboBox1 = new System.Windows.Forms.ComboBox();
this .listBox1 = new System.Windows.Forms.ListBox();
this .button1 = new System.Windows.Forms.Button();
this .SuspendLayout();
this .comboBox1.FormattingEnabled = true ;
this .comboBox1.Location = new System.Drawing.Point( 22 , 24 );
this .comboBox1.Name = " comboBox1 " ;
this .comboBox1.Size = new System.Drawing.Size( 233 , 20 );
this .comboBox1.TabIndex = 0 ;
this .comboBox1.SelectedIndexChanged += new System.EventHandler( this .comboBox1_SelectedIndexChanged);
this .comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this .listBox1.FormattingEnabled = true ;
this .listBox1.ItemHeight = 12 ;
this .listBox1.Location = new System.Drawing.Point( 22 , 50 );
this .listBox1.Name = " listBox1 " ;
this .listBox1.Size = new System.Drawing.Size( 233 , 196 );
this .listBox1.TabIndex = 1 ;
this .listBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
this .button1.Location = new System.Drawing.Point( 180 , 252 );
this .button1.Name = " button1 " ;
this .button1.Size = new System.Drawing.Size( 75 , 23 );
this .button1.TabIndex = 2 ;
this .button1.Text = " OK " ;
this .button1.UseVisualStyleBackColor = true ;
this .button1.Click += new System.EventHandler( this .button1_Click);
this .AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 284 , 293 );
this .Controls.Add( this .button1);
this .Controls.Add( this .listBox1);
this .Controls.Add( this .comboBox1);
this .MaximizeBox = false ;
this .MinimizeBox = false ;
this .Name = " Form1 " ;
this .Text = " BLL Class " ;
this .Load += new System.EventHandler( this .Form1_Load);
this .ResumeLayout( false );
}
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private dbRoot myMeta;
private IZeusInput zeusInput;
public Form1(dbRoot myMeta, IZeusInput zeusInput)
{
this .myMeta = myMeta;
this .zeusInput = zeusInput;
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
IDatabase database = this .comboBox1.SelectedValue as IDatabase;
if (database != null )
{
this .listBox1.DataSource = database.Tables;
this .listBox1.DisplayMember = " Name " ;
}
}
private void Form1_Load( object sender, EventArgs e)
{
// 获取数据库,传给ComboBox
comboBox1.DataSource = this .myMeta.Databases;
// ComboBox显示数据库名
this .comboBox1.DisplayMember = " Name " ;
if ( this .myMeta.DefaultDatabase != null )
{
// 选中默认的数据库
this .comboBox1.SelectedIndex = this .comboBox1.FindStringExact( this .myMeta.DefaultDatabase.Name);
// 通过IDatabase 的Tables属性取得数据库里面所有的数据表
// 作为数据源传给ListBox
this .listBox1.DataSource = this .myMeta.DefaultDatabase.Tables;
// ListBox显示数据表的名字
this .listBox1.DisplayMember = " Name " ;
}
}
private void button1_Click( object sender, EventArgs e)
{
IDatabase database = this .comboBox1.SelectedValue as IDatabase;
ListBox.SelectedObjectCollection tables = this .listBox1.SelectedItems ;
this .zeusInput[ " databaseNames " ] = database.Name;
this .zeusInput[ " tableNames " ] = tables; // 不用自己手动选择该处理哪些表 全部表!
this .DialogResult = DialogResult.OK;
this .Close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
// 选中的表
private ITables _selectedTables;
// 数据库名
private string _dbName;
// 表名
private string _tableName;
// 类名
private string _className;
// 输出路径
private string _exportPath = " C:\\cs " ;
// 文件名
private string _fileName;
// 文件名
private string _nameSpace;
public GeneratedTemplate(ZeusContext context) : base (context) {}
// ---------------------------------------------------
// Render()就是Template Code的入口函数,当成Main就好了
// ---------------------------------------------------
public override void Render()
{
_dbName = input[ " databaseNames " ].ToString(); // 选择的数据库
// _selectedTables = input["tableName"] as ITables; // 不知道怎么取得所有的表 很是郁闷 暂时用所有的表好了
ITables _selectedTables = MyMeta.Databases[_dbName].Tables;
foreach (Table _newTable in _selectedTables)
{
ITable _workingTable = _newTable;
_tableName = _newTable.Alias.Replace( " " , "" );
_className = TableToClassName(_workingTable);
GenerateModelFile(_workingTable.Columns);
}
System.Diagnostics.Process.Start( " explorer.exe " ,_exportPath); // 文件输出完毕,打开输出文件夹
}
// ---------------------------------------------------
// 循环某个表中的所有列 每运行一次 则把一个表实体映射到一个cs文件
// ---------------------------------------------------
private void GenerateModelFile( IColumns Columns )
{
_nameSpace = " MyModel " ;
output.writeln( " using System; " );
output.writeln( "" );
output.writeln( " /// <summary> " );
output.writeln( " ///\tMyGeneration : 实体类 " + _className);
Version();
output.writeln( " /// </summary> " );
output.writeln( " namespace " + _nameSpace + " .Model " );
output.writeln( " { " );
output.writeln( " \tpublic class " + _className);
output.writeln( " \t{ " );
// 私有成员变量
// BuildPrivateMembers( Columns );
// BuildDefaultConstructor( Columns );
// BuildPublicProperties( Columns );
output.writeln( " \t} " );
output.writeln( " } " );
_fileName = " MOD_ " + _className + " .cs " ;
output.saveEnc( " c:\\cs\\ " + _fileName , " o " , " unicode " );
output.clear();
}
// 版本信息 【公共小函数】
private void Version()
{
output.writeln( " ///\t作者:You " );
output.writeln( " ///\t我的博客:http://you.cnblogs.com/ " );
output.writeln( " ///\t版本:1.0 " );
output.writeln( " ///\tCreated on " + DateTime.Now);
}
// 表转换为类名【公共小函数】
protected string TableToClassName(ITable table)
{
string tableName = table.Alias;
return tableName.Replace( " " , "" );
}
}
%>
public class GeneratedTemplate : DotNetScriptTemplate
{
// 选中的表
private ITables _selectedTables;
// 数据库名
private string _dbName;
// 表名
private string _tableName;
// 类名
private string _className;
// 输出路径
private string _exportPath = " C:\\cs " ;
// 文件名
private string _fileName;
// 文件名
private string _nameSpace;
public GeneratedTemplate(ZeusContext context) : base (context) {}
// ---------------------------------------------------
// Render()就是Template Code的入口函数,当成Main就好了
// ---------------------------------------------------
public override void Render()
{
_dbName = input[ " databaseNames " ].ToString(); // 选择的数据库
// _selectedTables = input["tableName"] as ITables; // 不知道怎么取得所有的表 很是郁闷 暂时用所有的表好了
ITables _selectedTables = MyMeta.Databases[_dbName].Tables;
foreach (Table _newTable in _selectedTables)
{
ITable _workingTable = _newTable;
_tableName = _newTable.Alias.Replace( " " , "" );
_className = TableToClassName(_workingTable);
GenerateModelFile(_workingTable.Columns);
}
System.Diagnostics.Process.Start( " explorer.exe " ,_exportPath); // 文件输出完毕,打开输出文件夹
}
// ---------------------------------------------------
// 循环某个表中的所有列 每运行一次 则把一个表实体映射到一个cs文件
// ---------------------------------------------------
private void GenerateModelFile( IColumns Columns )
{
_nameSpace = " MyModel " ;
output.writeln( " using System; " );
output.writeln( "" );
output.writeln( " /// <summary> " );
output.writeln( " ///\tMyGeneration : 实体类 " + _className);
Version();
output.writeln( " /// </summary> " );
output.writeln( " namespace " + _nameSpace + " .Model " );
output.writeln( " { " );
output.writeln( " \tpublic class " + _className);
output.writeln( " \t{ " );
// 私有成员变量
// BuildPrivateMembers( Columns );
// BuildDefaultConstructor( Columns );
// BuildPublicProperties( Columns );
output.writeln( " \t} " );
output.writeln( " } " );
_fileName = " MOD_ " + _className + " .cs " ;
output.saveEnc( " c:\\cs\\ " + _fileName , " o " , " unicode " );
output.clear();
}
// 版本信息 【公共小函数】
private void Version()
{
output.writeln( " ///\t作者:You " );
output.writeln( " ///\t我的博客:http://you.cnblogs.com/ " );
output.writeln( " ///\t版本:1.0 " );
output.writeln( " ///\tCreated on " + DateTime.Now);
}
// 表转换为类名【公共小函数】
protected string TableToClassName(ITable table)
{
string tableName = table.Alias;
return tableName.Replace( " " , "" );
}
}
%>