------------------------------------------------------------------------
1)界面就用软件本身封装的
2)可以选择当前选择的数据库中的几个表
3) 同MyGeneration【ui-winform】做一下对比而已
-----------------------------------------------------------------------
Interface Code
<%
#REFERENCE System.Windows.Forms.dll
%>
<% #NAMESPACE System.Windows.Forms, System.Drawing,System, System.Text, System.Collections, Zeus, Zeus.UserInterface, Zeus.DotNetScript, Microsoft.Win32 %>
public class GeneratedGui : DotNetScriptGui
{
public GeneratedGui( ZeusGuiContext context ) : base ( context ) {}
GuiTextBox classNamespace;
GuiTextBox outputPath;
GuiTextBox memberPrefix;
GuiTextBox connectionString;
public override void Setup()
{
if ( ! input.Contains( " chooseTables " ) || ! input.Contains( " txtPath " ) ||
( ! input.Contains( " chkClass " ) && ! input.Contains( " chkNaming " ) ) )
{
ui.Title = " Zhuer's Generator " ;
ui.Width = 330 ;
ui.Height = 400 ;
// 选择数据库Label
GuiLabel label5 = ui.AddLabel( " label5 " , " 选择一个数据库: " , " 选择一个数据库 " );
// 选择数据库下拉列表
GuiComboBox chooseDatabase = ui.AddComboBox( " chooseDatabase " , " 选择一个数据库 " );
// 显示当前选择的数据库中所包含的表
GuiLabel label7 = ui.AddLabel( " label7 " , " 选择表: " , " 选择表 " );
GuiListBox chooseTables = ui.AddListBox( " chooseTables " , " 选择表 " );
chooseTables.Height = 200 ;
// 为选择数据库的下拉列表创建onchange事件
setupDatabaseDropdown( chooseDatabase );
// 绑定处理方法
chooseDatabase.AttachEvent( " onchange " , " chooseDatabase_onchange " );
ui.ShowGui = true ;
}
else
{
ui.ShowGui = false ;
}
}
// 选择数据库
public void setupDatabaseDropdown( GuiComboBox Databases )
{
try
{
if ( MyMeta.IsConnected )
{
Databases.BindData( MyMeta.Databases );
if ( MyMeta.DefaultDatabase != null )
{
Databases.SelectedValue = MyMeta.DefaultDatabase.Alias;
bindTables( Databases.SelectedValue );
}
}
}
catch
{
}
}
// 绑定数据库中的表
public void bindTables( string sDatabase )
{
int count = 0 ;
GuiListBox lstTables = ui[ " chooseTables " ] as GuiListBox;
try
{
IDatabase db = MyMeta.Databases[sDatabase];
lstTables.BindData( db.Tables );
}
catch
{
}
}
// 选择数据库引发的事件
public void chooseDatabase_onchange( GuiComboBox control )
{
int count = 0 ;
GuiComboBox cmbDatabases = ui[ " chooseDatabase " ] as GuiComboBox;
bindTables( cmbDatabases.SelectedText );
}
}
<% #NAMESPACE System.Windows.Forms, System.Drawing,System, System.Text, System.Collections, Zeus, Zeus.UserInterface, Zeus.DotNetScript, Microsoft.Win32 %>
public class GeneratedGui : DotNetScriptGui
{
public GeneratedGui( ZeusGuiContext context ) : base ( context ) {}
GuiTextBox classNamespace;
GuiTextBox outputPath;
GuiTextBox memberPrefix;
GuiTextBox connectionString;
public override void Setup()
{
if ( ! input.Contains( " chooseTables " ) || ! input.Contains( " txtPath " ) ||
( ! input.Contains( " chkClass " ) && ! input.Contains( " chkNaming " ) ) )
{
ui.Title = " Zhuer's Generator " ;
ui.Width = 330 ;
ui.Height = 400 ;
// 选择数据库Label
GuiLabel label5 = ui.AddLabel( " label5 " , " 选择一个数据库: " , " 选择一个数据库 " );
// 选择数据库下拉列表
GuiComboBox chooseDatabase = ui.AddComboBox( " chooseDatabase " , " 选择一个数据库 " );
// 显示当前选择的数据库中所包含的表
GuiLabel label7 = ui.AddLabel( " label7 " , " 选择表: " , " 选择表 " );
GuiListBox chooseTables = ui.AddListBox( " chooseTables " , " 选择表 " );
chooseTables.Height = 200 ;
// 为选择数据库的下拉列表创建onchange事件
setupDatabaseDropdown( chooseDatabase );
// 绑定处理方法
chooseDatabase.AttachEvent( " onchange " , " chooseDatabase_onchange " );
ui.ShowGui = true ;
}
else
{
ui.ShowGui = false ;
}
}
// 选择数据库
public void setupDatabaseDropdown( GuiComboBox Databases )
{
try
{
if ( MyMeta.IsConnected )
{
Databases.BindData( MyMeta.Databases );
if ( MyMeta.DefaultDatabase != null )
{
Databases.SelectedValue = MyMeta.DefaultDatabase.Alias;
bindTables( Databases.SelectedValue );
}
}
}
catch
{
}
}
// 绑定数据库中的表
public void bindTables( string sDatabase )
{
int count = 0 ;
GuiListBox lstTables = ui[ " chooseTables " ] as GuiListBox;
try
{
IDatabase db = MyMeta.Databases[sDatabase];
lstTables.BindData( db.Tables );
}
catch
{
}
}
// 选择数据库引发的事件
public void chooseDatabase_onchange( GuiComboBox control )
{
int count = 0 ;
GuiComboBox cmbDatabases = ui[ " chooseDatabase " ] as GuiComboBox;
bindTables( cmbDatabases.SelectedText );
}
}
Template Code
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
// 选中的表
private IList _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[ " chooseDatabase " ].ToString(); // 选择的数据库
_selectedTables = input[ " chooseTables " ] as ArrayList; // 选择的表
foreach ( string _newTable in _selectedTables)
{
ITable _workingTable = MyMeta.Databases[_dbName].Tables[_newTable];
_tableName = _workingTable.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 = _className + " .cs " ;
output.saveEnc( " c:\\cs\\ " + " MOD_ " + _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 IList _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[ " chooseDatabase " ].ToString(); // 选择的数据库
_selectedTables = input[ " chooseTables " ] as ArrayList; // 选择的表
foreach ( string _newTable in _selectedTables)
{
ITable _workingTable = MyMeta.Databases[_dbName].Tables[_newTable];
_tableName = _workingTable.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 = _className + " .cs " ;
output.saveEnc( " c:\\cs\\ " + " MOD_ " + _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( " " , "" );
}
}
%>