<p> 前段时间用C#做网站,用到了大量数据库相关的东西。网站采用3层结构,即数据访问层(Data Access
Layer),业务逻辑层(Business Logic
Layer),页面表现层().做了一段时间,发现向数据访问层和业务逻辑层加入新的类,数据库的表结构改了,还要对应的修改数据访问层和业务逻辑层的代
码,这个工作很是繁琐,无聊,而且容易出错。做了几次之后就想有什么办法可以让机器自动完成呢?</p>
<p>
联想到以前看过Java似乎有个Hibernate,可以很方便的实现对象关系映射(ORM),即自动的从数据库的表生成对应的对象,.Net也应该有类
似的功能吧。于是找啊找,发现了很多.Net的ORM工具,不过都有缺点,就是代码得依赖于那些ORM工具,我希望能够让机器按我的要求生成我自己的代
码,这样就更加灵活了。</p>
<p> 于是乎,发现了CodeSmith和MyGeneration,CodeSmith是 网上传的.NET
程序员十种必备工具之一,我们写代码时,经常需要重复完成某些特定的任务,例如编写数据访问代码或者生成自定义集合。我们可以用CodeSmith编写模
板自动完成这些任务,从而不仅提高工作效率,而且能够自动完成那些最为乏味的任务。可惜,CodeSmith是需要注册的,试用版只能用15天。而
MyGeneration基本上和CodeSmith的功能差不多哦,但是他是开源的。我选软件的原则是能开源免费的就用,实在没替代了才选那些需要注册
的,有版权的软件。所以就选MyGeneration了。</p>
<p>
用过一段时间后感觉MyGeneration主要是为了自动生成数据库相关的代码的,可能C#用得比较多,其实我们可以用它生成任何代
码,C++,JavaScript...而且还不仅仅局限于数据库,其他方面的代码也可以用MyGeneration自动生成。比如我们经常用数据访问层
和业务逻辑层,用MyGeneration就可以自动生成这些代码,我们可以不用手动写代码了。比如数据访问层,我们需要调用一个存储过程,用
MyGeneration我们只需要选择生成存储过程代码的模板,执行一下脚本,然后在界面上选择数据库上某个存储过程,然后就自动生成了数据库访问代
码,整个过程只需要点几下鼠标,代码就自动生成了。这对于需要大量操作数据库的程序员来说,效率是多大的提升啊。</p>
<p> 废话少说,还是来点实在的吧。首先声明,我的MyGeneration版本是:1.3.0.3</p>
<p> 安装完MyGeneration后,第一次启动会要求进行一些数据库相关的配置。如图:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/Config.PNG" alt=""></p>
<p> ConnectionString: 就是指定连接哪个数据库了,填好这个就可以点确定了。</p>
<p> 下面来看一看其他的项都是什么。</p>
<p> Language Mapping:就是指定数据库和对象基本类型的映射关系。让我们打开Languages.xml文件看一下吧:</p>
<div class="highlighter"><ol class="highlighter-xml">
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Language</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"SQL"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"C#"</span>
<span class="tag">></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"bigint"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"long"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"binary"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"object"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"bit"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"bool"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"char"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"datetime"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DateTime"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"float"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"double"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"image"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte[]"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"int"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"int"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"money"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"nchar"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"ntext"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"numeric"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"nvarchar"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"real"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"float"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"smalldatetime"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DateTime"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"smallint"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"short"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"smallmoney"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"text"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"timestamp"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte[]"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"tinyint"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"uniqueidentifier"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"Guid"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"varbinary"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte[]"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"varchar"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"xml"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"sql_variant"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"object"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"></</span>
<span class="tag-name">Language</span>
<span class="tag">></span>
</li>
</ol></div>
<p> 这是里面的一段内容,很明显,是数据库SQL的字段转到C#是什么类型,里面没有C++的,假如我们要让它支持C++的话,需要在这里加入SQL到C++的类型转换。</p>
<p> Database Target Mapping:先看里面的内容吧:</p>
<div class="highlighter"><ol class="highlighter-xml">
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">DbTarget</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"ACCESS"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO"</span>
<span class="tag">></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Text"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbText"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Memo"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbMemo"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"DateTime"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbDate"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Currency"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbCurrency"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Yes/No"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbBoolean"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"OLEObject"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbLongBinary"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Hyperlink"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbMemo"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Double"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbDouble"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"ReplicationID"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbGUID"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Long"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbLong"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Single"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbSingle"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Decimal"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbDecimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Byte"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbByte"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Integer"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbInteger"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"></</span>
<span class="tag-name">DbTarget</span>
<span class="tag">></span>
</li>
</ol></div>
<p> 呵呵,一目了然,就是Access数据库用DAO的方式访问,数据库的列的类型对应的DAO里是什么类型。</p>
<p> UseMetaData目前没什么用。</p>
<p> 看看MyGeneration的界面吧:</p>
<p> <img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/FrameWork.PNG" alt=""></p>
<p> Template Browser 面板 列出了一些模板,这是自动生成代码需要用到的模板。安装的时候会自己带很多模板。</p>
<p> MyMeta Browser则列出了当前连接的数据库上有些什么库,表,存储过程。</p>
<p> 工作区则是具体模板对应的代码。</p>
<p></p>
<p> 先让我们体验一下吧。</p>
<p> 展开Template Browser面板下"d00dads - C#", 双击 “d00dads - Invoke a Stored Procedure", 让工作区显示其内容,</p>
<p>然后点击工具栏上的 "Execute" 按钮,如图红框所示:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/Exe.PNG" alt=""></p>
<p> 弹出对话框,如图:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/CreateSP.PNG" alt=""></p>
<p> 选择数据库,存储过程,存储过程类型,点确定(OK)。</p>
<p></p>
<p> 然后可以看到工作区 Output 里输出了代码了。例如:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System.Collections.Specialized;</span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Data.SqlClient;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">void</span>
<span>dm_exec_cursors(</span>
<span class="keyword">int</span>
<span>spid)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>ListDictionaryparameters=</span>
<span class="keyword">new</span>
<span>ListDictionary();</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>parameters.Add(</span>
<span class="keyword">new</span>
<span>SqlParameter(</span>
<span class="string">"@spid"</span>
<span>,SqlDbType.Int,0),spid);</span>
</li>
<li>
<span>LoadFromSqlNoExec(</span>
<span class="string">"dm_exec_cursors"</span>
<span>,parameters);</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 这就是MyGeneration自动获取了存储过程的输入参数,然后在代码里构造相应的参数,然后生成的代码。</p>
<p> 这只是MyGeneration自带的模板生成的,大家可以试一试其他的模板的效果。</p>
<p> 里面有自动根据表结构生成BLL的类......看下效果:</p>
<div class="highlighter"><ol class="highlighter-c">
<li class="alt">
<span class="comment">/*</span>
</li>
<li>
<span class="comment">'===============================================================================</span>
</li>
<li class="alt">
<span class="comment">'GeneratedFrom-CSharp_dOOdads_BusinessEntity.vbgen</span>
</li>
<li>
<span class="comment">'</span>
</li>
<li class="alt">
<span class="comment">'**IMPORTANT**</span>
</li>
<li>
<span class="comment">'HowtoGenerateyourstoredprocedures:</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'SQL=SQL_StoredProcs.vbgen</span>
</li>
<li class="alt">
<span class="comment">'ACCESS=Access_StoredProcs.vbgen</span>
</li>
<li>
<span class="comment">'ORACLE=Oracle_StoredProcs.vbgen</span>
</li>
<li class="alt">
<span class="comment">'FIREBIRD=FirebirdStoredProcs.vbgen</span>
</li>
<li>
<span class="comment">'POSTGRESQL=PostgreSQL_StoredProcs.vbgen</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'ThesupportingbaseclassOleDbEntityisintheArchitecturedirectoryin"dOOdads".</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'Thisobjectis'abstract'whichmeansyouneedtoinheritfromittobeable</span>
</li>
<li class="alt">
<span class="comment">'toinstantiateit.Thisisveryeasillydone.Youcanoverridepropertiesand</span>
</li>
<li>
<span class="comment">'methodsinyourderivedclass,thisallowsyoutoregeneratethisclassatany</span>
</li>
<li class="alt">
<span class="comment">'timeandnotworryaboutoverwritingcustomcode.</span>
</li>
<li>
<span class="comment">'</span>
</li>
<li class="alt">
<span class="comment">'NEVEREDITTHISFILE.</span>
</li>
<li>
<span class="comment">'</span>
</li>
<li class="alt">
<span class="comment">'publicclassYourObject:_YourObject</span>
</li>
<li>
<span class="comment">'{</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'}</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'===============================================================================</span>
</li>
<li class="alt">
<span class="comment">*/</span>
</li>
<li class="alt">
<span class="comment">//GeneratedbyMyGenerationVersion#(1.3.0.3)</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>System;</span>
</li>
<li>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>System.Data.OleDb;</span>
</li>
<li>
<span class="keyword">using</span>
<span>System.Collections;</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>System.Collections.Specialized;</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>MyGeneration.dOOdads;</span>
</li>
<li class="alt">
<span class="keyword">namespace</span>
<span>Your.Namespace</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">abstract</span>
<span></span>
<span class="keyword">class</span>
<span>_Users:OleDbEntity</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>_Users()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.QuerySource=</span>
<span class="string">"Users"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.MappingName=</span>
<span class="string">"Users"</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//publicOverridesvoidAddNew()</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>AddNew()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.AddNew();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">string</span>
<span>GetAutoKeyColumn()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="string">"ID"</span>
<span>;</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>FlushData()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>._whereClause=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>._aggregateClause=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.FlushData();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//publicFunctionLoadAll()AsBoolean</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//Loadsalloftherecordsinthedatabase,andsetsthecurrentRowtothefirstrow</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">bool</span>
<span>LoadAll()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ListDictionaryparameters=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.LoadFromSql(</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersLoadAll]"</span>
<span>,parameters);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//publicOverridableFunctionLoadByPrimaryKey()AsBoolean</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//Loadsasinglerowofviatheprimarykey</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">bool</span>
<span>LoadByPrimaryKey()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ListDictionaryparameters=</span>
<span class="keyword">new</span>
<span>ListDictionary();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.LoadFromSql(</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersLoadByPrimaryKey]"</span>
<span>,parameters);</span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">
<span class="preprocessor"></span>
</li>
<li>
<span class="preprocessor">#regionParameters</span>
</li>
<li class="alt">
<span class="keyword">protected</span>
<span></span>
<span class="keyword">class</span>
<span>Parameters</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">static</span>
<span>OleDbParameterID</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">new</span>
<span>OleDbParameter(</span>
<span class="string">"@ID"</span>
<span>,OleDbType.Integer,0);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">static</span>
<span>OleDbParameterAlias</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">new</span>
<span>OleDbParameter(</span>
<span class="string">"@Alias"</span>
<span>,OleDbType.VarChar,2147483647);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">#endregion<span>
</span>
</li>
<li>
<span class="preprocessor"></span>
</li>
<li class="alt">
<span class="preprocessor">#regionColumnNames</span>
</li>
<li>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>ColumnNames</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>ID=</span>
<span class="string">"ID"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>Alias=</span>
<span class="string">"Alias"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">string</span>
<span>ToPropertyName(</span>
<span class="keyword">string</span>
<span>columnName)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(ht==</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ht=</span>
<span class="keyword">new</span>
<span>Hashtable();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>ht[ID]=_Users.PropertyNames.ID;</span>
</li>
<li>
<span>ht[Alias]=_Users.PropertyNames.Alias;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>(</span>
<span class="keyword">string</span>
<span>)ht[columnName];</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">private</span>
<span>Hashtableht=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li>#endregion<span>
</span>
</li>
<li class="alt">
<span class="preprocessor"></span>
</li>
<li>
<span class="preprocessor">#regionPropertyNames</span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>PropertyNames</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>ID=</span>
<span class="string">"ID"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>Alias=</span>
<span class="string">"Alias"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">string</span>
<span>ToColumnName(</span>
<span class="keyword">string</span>
<span>propertyName)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(ht==</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>ht=</span>
<span class="keyword">new</span>
<span>Hashtable();</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>ht[ID]=_Users.ColumnNames.ID;</span>
</li>
<li class="alt">
<span>ht[Alias]=_Users.ColumnNames.Alias;</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span>(</span>
<span class="keyword">string</span>
<span>)ht[propertyName];</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">private</span>
<span>Hashtableht=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">#endregion<span>
</span>
</li>
<li class="alt">
<span class="preprocessor">#regionStringPropertyNames</span>
</li>
<li>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>StringPropertyNames</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>ID=</span>
<span class="string">"s_ID"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>Alias=</span>
<span class="string">"s_Alias"</span>
<span>;</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li>#endregion<span>
</span>
</li>
<li class="alt">
<span class="preprocessor"></span>
</li>
<li>
<span class="preprocessor">#regionProperties</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span>IntegerID</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.GetInteger(ColumnNames.ID);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">base</span>
<span>.SetInteger(ColumnNames.ID,value);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span>StringAlias</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.GetString(ColumnNames.Alias);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">base</span>
<span>.SetString(ColumnNames.Alias,value);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">#endregion<span>
</span>
</li>
<li>
<span class="preprocessor"></span>
</li>
<li class="alt">
<span class="preprocessor">#regionStringProperties</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">string</span>
<span>s_ID</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">get</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">this</span>
<span>.IsColumnNull(ColumnNames.ID)?</span>
<span class="keyword">string</span>
<span>.Empty:</span>
<span class="keyword">base</span>
<span>.GetIntegerAsString(ColumnNames.ID);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">set</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">string</span>
<span>.Empty==value)</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.SetColumnNull(ColumnNames.ID);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">else</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.ID=</span>
<span class="keyword">base</span>
<span>.SetIntegerAsString(ColumnNames.ID,value);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">string</span>
<span>s_Alias</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">this</span>
<span>.IsColumnNull(ColumnNames.Alias)?</span>
<span class="keyword">string</span>
<span>.Empty:</span>
<span class="keyword">base</span>
<span>.GetStringAsString(ColumnNames.Alias);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">string</span>
<span>.Empty==value)</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.SetColumnNull(ColumnNames.Alias);</span>
</li>
<li>
<span></span>
<span class="keyword">else</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Alias=</span>
<span class="keyword">base</span>
<span>.SetStringAsString(ColumnNames.Alias,value);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li>#endregion</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>AggregateClause_aggregateClause=</span>
<span class="keyword">null</span>
<span>;
</span>
</li>
<li>
<span class="preprocessor">#endregion</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span>IDbCommandGetInsertCommand()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbCommandcmd=</span>
<span class="keyword">new</span>
<span>OleDbCommand();</span>
</li>
<li>
<span>cmd.CommandType=CommandType.StoredProcedure;</span>
</li>
<li class="alt">
<span>cmd.CommandText=</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersInsert]"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>CreateParameters(cmd);</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span>IDbCommandGetUpdateCommand()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbCommandcmd=</span>
<span class="keyword">new</span>
<span>OleDbCommand();</span>
</li>
<li>
<span>cmd.CommandType=CommandType.StoredProcedure;</span>
</li>
<li class="alt">
<span>cmd.CommandText=</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersUpdate]"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>CreateParameters(cmd);</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span>IDbCommandGetDeleteCommand()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbCommandcmd=</span>
<span class="keyword">new</span>
<span>OleDbCommand();</span>
</li>
<li>
<span>cmd.CommandType=CommandType.StoredProcedure;</span>
</li>
<li class="alt">
<span>cmd.CommandText=</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersDelete]"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbParameterp;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>IDbCommandCreateParameters(OleDbCommandcmd)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>OleDbParameterp;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>p=cmd.Parameters.Add(Parameters.ID);</span>
</li>
<li class="alt">
<span>p.SourceColumn=ColumnNames.ID;</span>
</li>
<li>
<span>p.SourceVersion=DataRowVersion.Current;</span>
</li>
<li>
<span>p=cmd.Parameters.Add(Parameters.Alias);</span>
</li>
<li class="alt">
<span>p.SourceColumn=ColumnNames.Alias;</span>
</li>
<li>
<span>p.SourceVersion=DataRowVersion.Current;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 这就是自动获得表结构,然后从字段映射成类里面的成员,并且还有一些插入,更新,删除的代码。</p>
<p> 当然自带的模板生成的代码不一定符合我们的需要,但是我们可以根据需要自己写一些模板来生成符合自己需要的代码,这也是非常容易的事,欲知如何实现,请看下回文章。</p>
<p>===========================================================</p>
<p>要用MyGeneration就必须要和各种模板打交道。我们可以使用别人写的模板来完成我们说需要的功能,但是别人写的模板不一定最适合我们的项目里面的代码,所以有时也需要自己写一些模板来使用。下面就讲如何编写模板吧<br>
通过File – New 菜单我们可以看到,MyGeneration支持的模板包括C#,VB.Net,Jscript,VBScript,我们可以选则自己擅长的语言来写模板。<br>
最简单的办法就是找一个功能差不多的模板,然后在这个模板的基础上进行修改了。这个也是初学的办法,可以快速的了解如何实现特定的功能。<br><br>
当然,我们要自己建一个模板,以C#模板为例吧。<br>
假如我们要自己生成一个数据库的数据表所对应的BLL类。<br>
在UI界面上需要我们选择是哪个数据库,那张数据表。最后生成BLL类。<br><br>
选择菜单 File – New – C# Zeus
Template,就打开了一个新的工作区,该模板的代码是C#的。工作区有5个Tab页,Template Code, Interface
Code, Template Source, Interface Source, Output.如下图所示: </p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080903/MyGeneration1.PNG" alt=""></p>
<p> 这些都干什么的呢?<br>
这得了解一下MyGeneration内部的结构了。MyGeneration通过脚本引擎执行Template Code和Interface
Code中的脚本,生成Template Source和Interface Source的代码,然后执行Interface
Source显示用户界面,再执行Template Source,输出结果到Output中。<br>
由此可见,Template
Source是根据Template code生成的Interface Source是根据Interface
code生成的,他们是不可编辑的。我们能用代码控制的就是Template code和Interface code了。而Interface
code是主要和用户界面有关的,Template code则主要控制执行完用户界面的代码后如何输出到Output。<br><br>
默认生成的Interface Code如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedGui:DotNetScriptGui</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span>GeneratedGui(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li>
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//TheUserInterfaceEntryPoint</span>
</li>
<li>
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Setup()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//**UNCOMMENTCODEBELOWTOSEEUI**</span>
</li>
<li>
<span></span>
<span class="comment">//ui.Width=100;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.Height=100;</span>
</li>
<li>
<span></span>
<span class="comment">//GuiLabellblDemo=ui.AddLabel("lblDemo","Demo","DemoTooltip");</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.ShowGui=true;</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p><br>
Interface Code中编写用户界面有两种方法,一种是采用MyGeneration提供的GUI库,另外一种则是完全采用C#本身的界面代码编写。下图显示了MyGeneration的GUI库的帮助,从中可以看到其提供的各种GUI类。</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080903/MyGenerationGUI.PNG" alt=""><br></p>
<p> 我可不想为了写个模板去学习使用MyGeneration的GUI库,这可加大了学习成本,而且所有界面都得用代码一行一行的写出来,多麻烦啊。如果能像C#那样,直接用鼠标拖拽控件就能搞定界面UI就好了。<br>
其
实,我们是可以用C#的界面库的。我的方法是这样的。先自己用VS新建一个Windows的工程,然后在窗体上摆好控件,设置各种控件的属性,并且把需要
用的各种控件的事件都让VS的IDE生成好。然后把这个窗体的代码直接拷贝到MyGeneration的Interface
code里面去。注意,假设我们的窗体叫Form1,我们需要拷贝Form1.Designer.cs 和Form1.cs两个文件中的代码。<br><br>
然后在Interface Code最前面加入下面两行:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%#REFERENCESystem.Windows.Forms.dll,System.Drawing.dll%></span>
</li>
<li class="alt">
<span><%#NAMESPACESystem.Windows.Forms,System.Drawing%></span>
</li>
</ol></div>
<p><br>
在Setup()函数中写:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span>Form1form=</span>
<span class="keyword">new</span>
<span>Form1();</span>
</li>
<li class="alt">
<span class="keyword">if</span>
<span>(form.ShowDialog()!=DialogResult.OK)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ui.IsCanceled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p></p>
<p> 最后所形成的Interface Code的代码如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%#REFERENCESystem.Windows.Forms.dll,System.Drawing.dll%></span>
</li>
<li class="alt">
<span><%#NAMESPACESystem.Windows.Forms,System.Drawing%></span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedGui:DotNetScriptGui</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedGui(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="comment">//TheUserInterfaceEntryPoint</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Setup()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//**UNCOMMENTCODEBELOWTOSEEUI**</span>
</li>
<li>
<span></span>
<span class="comment">//ui.Width=100;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.Height=100;</span>
</li>
<li>
<span></span>
<span class="comment">//GuiLabellblDemo=ui.AddLabel("lblDemo","Demo","DemoTooltip");</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.ShowGui=true;</span>
</li>
<li>
<span>Form1form=</span>
<span class="keyword">new</span>
<span>Form1();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(form.ShowDialog()!=DialogResult.OK)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ui.IsCanceled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>Form1:Form</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///Requireddesignervariable.</span>
</li>
<li>
<span></span>
<span class="comment">///</summary></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.ComponentModel.IContainercomponents=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///<summary></span>
</li>
<li>
<span></span>
<span class="comment">///Cleanupanyresourcesbeingused.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="comment">///<paramname="disposing">trueifmanagedresourcesshouldbedisposed;otherwise,false.</param></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Dispose(</span>
<span class="keyword">bool</span>
<span>disposing)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(disposing&&(components!=</span>
<span class="keyword">null</span>
<span>))</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>components.Dispose();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.Dispose(disposing);</span>
</li>
<li>
<span>}
</span>
</li>
<li>#regionWindowsFormDesignergeneratedcode</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///RequiredmethodforDesignersupport-donotmodify</span>
</li>
<li>
<span></span>
<span class="comment">///thecontentsofthismethodwiththecodeeditor.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>InitializeComponent()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ComboBox();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ListBox();</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.Button();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.SuspendLayout();</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//comboBox1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,24);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Name=</span>
<span class="string">"comboBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,20);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.TabIndex=0;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndexChanged+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.comboBox1_SelectedIndexChanged);</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//listBox1</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.ItemHeight=12;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,50);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Name=</span>
<span class="string">"listBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,196);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.TabIndex=1;</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//button1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(180,252);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.Name=</span>
<span class="string">"button1"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(75,23);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.TabIndex=2;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Text=</span>
<span class="string">"OK"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.UseVisualStyleBackColor=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//Form1</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleDimensions=</span>
<span class="keyword">new</span>
<span>System.Drawing.SizeF(6F,12F);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.ClientSize=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(284,293);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.button1);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.listBox1);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.comboBox1);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.MaximizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.MinimizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Name=</span>
<span class="string">"Form1"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Text=</span>
<span class="string">"ZhouKai'sBLLClass"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Load+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.Form1_Load);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.ResumeLayout(</span>
<span class="keyword">false</span>
<span>);</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li class="alt">#endregion</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ComboBoxcomboBox1;</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ListBoxlistBox1;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.Buttonbutton1;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span>Form1()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>InitializeComponent();</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>comboBox1_SelectedIndexChanged(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li>
<span>{</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>Form1_Load(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 执行一下该模板,看有什么效果吧?是不是看到了如下的UI呢?</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080903/NoDataUI.PNG" alt=""></p>
<p> 呵呵,这个只有UI,还没有数据,下次该讲如何给这个UI加上他说需要的数据啦。比如如何获得数据库的各个数据表啊之内的,然后如何输出自己说需要BLL代码和存储过程的代码啦。</p>
<p> 呵呵,发现竟然有网站转载我的文章了,心里小小高兴一下,说明写的东西还是有点用的。</p>
<p>==============================================================</p>
<p>前面讲了MyGeneration的使用,以及如何自己写模板显示UI,现在开始讲如何将数据库的信息显示到UI上。<br>
在MyGeneraion脚本系统中,有一个全局变量,叫做MyMeta,他是dbRoot类型的。通过这个全局变量,我们可以获得数据库相关的信息。这个变量在Interface Code 和Template Code中都可以使用。<br>
从上节代码来看,我们建立Form窗口的时候,就把这个变量给传给了Form窗口<br>
如下代码所示:<br>
MyForm form = new MyForm(MyMeta, input);<br>
那么dbRoot类型的MyMeta变量都有哪些功能,有哪些函数,属性可以使用呢?<br>
我们可以查找帮助,通过MyGeneration菜单 “File – Help – MyMeta API Reference”可以打开其帮助,里面有dbRoot的详细介绍,如下图所示:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080910/dbRoot.PNG" alt=""><br><br>
大概看一下API,然后我们就可以修改我们的模板了,将其修改如下:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>Form1_Load(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//获取数据库,传给ComboBox</span>
</li>
<li class="alt">
<span>comboBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.Databases;</span>
</li>
<li>
<span></span>
<span class="comment">//ComboBox显示数据库名</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase!=</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//选中默认的数据库</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndex=</span>
<span class="keyword">this</span>
<span>.comboBox1.FindStringExact(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Name);</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//通过IDatabase的Tables属性取得数据库里面所有的数据表</span>
</li>
<li>
<span></span>
<span class="comment">//作为数据源传给ListBox</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Tables;</span>
</li>
<li>
<span></span>
<span class="comment">//ListBox显示数据表的名字</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
</li>
</ol></div>
<p> 就算没有注释,这代码也很好懂吧。<br>
呵呵,由此可见,我们可以通过MyMeta得到IDataBases,然后获得ITable,在通过ITable<br>
获得数据表的信息……反正呢,照这条线找下去,基本上数据库里有哪些表,表里有哪些字段,有哪些存储过程,任何信息都可以很容易的得到。当然了,在自己写模板之前,最好先大概看一下“MyMeta API Reference”,这样就写模板就会更得心应手了。<br>
下面接下来看Form点击OK之后的代码吧</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>button1_Click(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>IDatabasedatabase=</span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>IDatabase;</span>
</li>
<li class="alt">
<span>ITabletable=</span>
<span class="keyword">this</span>
<span>.listBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>ITable;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"databaseName"</span>
<span>]=database.Name;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"tableName"</span>
<span>]=table.Name;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.DialogResult=DialogResult.OK;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Close();</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p>
这段代码得重点讲一下了,因为Interface Code是关于UI的,那么UI执行完了之后,我们就需要用到Template
Code了,那么Template Code和Interface Code是如何联系在一起的呢? 我们在Template
Code里面如何知道Interface Code显示的UI中用户输入了什么,选择了什么?<br>
这用到了MyGeneration的一个非常重要的类IZeusInput。<br>
改接口的详细信息请看帮助 “File – help – Zeus API Reference”。<br>
在这里我们简单的把UI上选中的数据库的名字和数据表的名字存在了IZeusInput变量中。大家注意看下Form的IZeumInput是如何来的吧。<br>
这样,我们就完成了该模板的Interface Code的代码,最后形成的Interface Code就如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%#REFERENCESystem.Windows.Forms.dll,System.Drawing.dll%></span>
</li>
<li class="alt">
<span><%#NAMESPACESystem.Windows.Forms,System.Drawing%></span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedGui:DotNetScriptGui</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedGui(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="comment">//TheUserInterfaceEntryPoint</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Setup()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//**UNCOMMENTCODEBELOWTOSEEUI**</span>
</li>
<li>
<span></span>
<span class="comment">//ui.Width=100;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.Height=100;</span>
</li>
<li>
<span></span>
<span class="comment">//GuiLabellblDemo=ui.AddLabel("lblDemo","Demo","DemoTooltip");</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.ShowGui=true;</span>
</li>
<li>
<span>Form1form=</span>
<span class="keyword">new</span>
<span>Form1(MyMeta,input);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(form.ShowDialog()!=DialogResult.OK)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ui.IsCanceled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>Form1:Form</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///Requireddesignervariable.</span>
</li>
<li>
<span></span>
<span class="comment">///</summary></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.ComponentModel.IContainercomponents=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///<summary></span>
</li>
<li>
<span></span>
<span class="comment">///Cleanupanyresourcesbeingused.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="comment">///<paramname="disposing">trueifmanagedresourcesshouldbedisposed;otherwise,false.</param></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Dispose(</span>
<span class="keyword">bool</span>
<span>disposing)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(disposing&&(components!=</span>
<span class="keyword">null</span>
<span>))</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>components.Dispose();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.Dispose(disposing);</span>
</li>
<li>
<span>}
</span>
</li>
<li>#regionWindowsFormDesignergeneratedcode</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///RequiredmethodforDesignersupport-donotmodify</span>
</li>
<li>
<span></span>
<span class="comment">///thecontentsofthismethodwiththecodeeditor.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>InitializeComponent()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ComboBox();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ListBox();</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.Button();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.SuspendLayout();</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//comboBox1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,24);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Name=</span>
<span class="string">"comboBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,20);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.TabIndex=0;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndexChanged+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.comboBox1_SelectedIndexChanged);</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//listBox1</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.ItemHeight=12;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,50);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Name=</span>
<span class="string">"listBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,196);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.TabIndex=1;</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//button1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(180,252);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.Name=</span>
<span class="string">"button1"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(75,23);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.TabIndex=2;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Text=</span>
<span class="string">"OK"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.UseVisualStyleBackColor=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Click+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.button1_Click);</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//Form1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleDimensions=</span>
<span class="keyword">new</span>
<span>System.Drawing.SizeF(6F,12F);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.ClientSize=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(284,293);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.button1);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.listBox1);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.comboBox1);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.MaximizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.MinimizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Name=</span>
<span class="string">"Form1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Text=</span>
<span class="string">"ZhouKai'sBLLClass"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Load+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.Form1_Load);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.ResumeLayout(</span>
<span class="keyword">false</span>
<span>);</span>
</li>
<li>
<span>}
</span>
</li>
<li>#endregion</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ComboBoxcomboBox1;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ListBoxlistBox1;</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.Buttonbutton1;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>dbRootmyMeta;</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>IZeusInputzeusInput;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>Form1(dbRootmyMeta,IZeusInputzeusInput)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.myMeta=myMeta;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.zeusInput=zeusInput;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>InitializeComponent();</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>comboBox1_SelectedIndexChanged(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>IDatabasedatabase=</span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>IDatabase;</span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(database!=</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DataSource=database.Tables;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>Form1_Load(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//获取数据库,传给ComboBox</span>
</li>
<li>
<span>comboBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.Databases;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ComboBox显示数据库名</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase!=</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//选中默认的数据库</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndex=</span>
<span class="keyword">this</span>
<span>.comboBox1.FindStringExact(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Name);</span>
</li>
<li>
<span></span>
<span class="comment">//通过IDatabase的Tables属性取得数据库里面所有的数据表</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//作为数据源传给ListBox</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Tables;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ListBox显示数据表的名字</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>button1_Click(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>IDatabasedatabase=</span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>IDatabase;</span>
</li>
<li class="alt">
<span>ITabletable=</span>
<span class="keyword">this</span>
<span>.listBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>ITable;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"databaseName"</span>
<span>]=database.Name;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"tableName"</span>
<span>]=table.Name;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.DialogResult=DialogResult.OK;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Close();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
</ol></div>
<p> 下面来看Template Code如何写吧。目前的模板默认的Template Code如下所示:<br></p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%</span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedTemplate:DotNetScriptTemplate</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedTemplate(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//---------------------------------------------------</span>
</li>
<li>
<span></span>
<span class="comment">//Render()iswhereyouwanttowriteyourlogic</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//---------------------------------------------------</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Render()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>%></span>
</li>
<li class="alt">
<span>Youcantoggle</span>
<span class="keyword">in</span>
<span></span>
<span class="keyword">out</span>
<span>ofscriptlike</span>
<span class="keyword">this</span>
</li>
<li>
<span><%</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"Helloworld."</span>
<span>);</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>%></span>
</li>
</ol></div>
<p> C# Template Code的语法和Asp的语法十分的类似<%=%>表示绑定某一个字段或属性<br>
<%%>表示脚本段,我们可以在这里写符合C#语法的任何语句<br>
其他的内容不进行解析直接用output写到最后的结果里<br><br>
修改Template Code,改成如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%</span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedTemplate:DotNetScriptTemplate</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedTemplate(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Render()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//设置输出语言...</span>
</li>
<li>
<span>MyMeta.Language=</span>
<span class="string">"C#"</span>
<span>;</span>
</li>
<li class="alt">
<span>MyMeta.DbTarget=</span>
<span class="string">"SQLClient"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//获得执行完UI后,我们说选中的数据库名,数据表名</span>
</li>
<li>
<span></span>
<span class="keyword">string</span>
<span>databaseName=input[</span>
<span class="string">"databaseName"</span>
<span>].ToString();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">string</span>
<span>tableName=input[</span>
<span class="string">"tableName"</span>
<span>].ToString();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>IDatabasedatabase=MyMeta.Databases[databaseName];</span>
</li>
<li>
<span></span>
<span class="comment">//获得数据表的接口</span>
</li>
<li class="alt">
<span>ITabletable=database.Tables[tableName];</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>%></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span class="keyword">namespace</span>
<span>BLL</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System;</span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Collections;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System.Collections.Generic;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span><%</span>
</li>
<li>
<span></span>
<span class="comment">//数据表的名称作为BLL类的类名</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"publicpartialclass"</span>
<span>+tableName);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"{"</span>
<span>);</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="comment">//遍历数据表中所有字段</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">foreach</span>
<span>(IColumncolumn</span>
<span class="keyword">in</span>
<span>table.Columns)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//将字段名第一个字母转为小写</span>
</li>
<li>
<span></span>
<span class="keyword">string</span>
<span>tmpColumnName=DnpUtils.SetCamelCase(column.Name);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"private"</span>
<span>+column.LanguageType+</span>
<span class="string">"_"</span>
<span>+tmpColumnName+</span>
<span class="string">";"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"public"</span>
<span>+column.LanguageType+</span>
<span class="string">""</span>
<span>+column.Name);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"{"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"get{return_"</span>
<span>+tmpColumnName+</span>
<span class="string">";}"</span>
<span>);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"set{_"</span>
<span>+tmpColumnName+</span>
<span class="string">"=value;}"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"}"</span>
<span>);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">""</span>
<span>);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"}"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"}"</span>
<span>);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span>%></span>
</li>
</ol></div>
<p> 这代码也非常简单,基本不用解释都能看懂,不用几分钟就能自己写出来了。<br>
运行一下该模板,看下是不是类似有如下输出呢?</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span class="keyword">namespace</span>
<span>BLL</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>partial</span>
<span class="keyword">class</span>
<span>User</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">int</span>
<span>_id;</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">int</span>
<span>ID</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
<span>{</span>
<span class="keyword">return</span>
<span>_id;}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">set</span>
<span>{_id=value;}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">string</span>
<span>_userName;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">string</span>
<span>UserName</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">get</span>
<span>{</span>
<span class="keyword">return</span>
<span>_userName;}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
<span>{_userName=value;}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 好了,最简单的一个生成BLL类的模板就完成了。<br>
本来还想写下如何用MyGeneration来生成执行存储过程的函数,如何用MyGeneration来生成测试存储过程函数的代码的。我觉得完成了这个模板,基本上就可以自己查找MyGeneration的帮助中的API完成这些了,应该不用继续下去了。<br>
继续意淫一下,假如写数据库相关的程序,有了MyGeneration,我们的工作就简单多了,我们可以自己写模板,自动生成BLL类,DAL代码,自动生成测试代码……多棒啊</p>
<p></p>
<p>本文来自<br><a style="font-size: 15pt;" href="http://blog.csdn.net/zxcred">http://blog.csdn.net/zxcred</a>
</p>
<p></p>
<p>MyGeneration下载地址</p>
<p>http://download.csdn.net/source/1328655</p>
<p></p>
Layer),业务逻辑层(Business Logic
Layer),页面表现层().做了一段时间,发现向数据访问层和业务逻辑层加入新的类,数据库的表结构改了,还要对应的修改数据访问层和业务逻辑层的代
码,这个工作很是繁琐,无聊,而且容易出错。做了几次之后就想有什么办法可以让机器自动完成呢?</p>
<p>
联想到以前看过Java似乎有个Hibernate,可以很方便的实现对象关系映射(ORM),即自动的从数据库的表生成对应的对象,.Net也应该有类
似的功能吧。于是找啊找,发现了很多.Net的ORM工具,不过都有缺点,就是代码得依赖于那些ORM工具,我希望能够让机器按我的要求生成我自己的代
码,这样就更加灵活了。</p>
<p> 于是乎,发现了CodeSmith和MyGeneration,CodeSmith是 网上传的.NET
程序员十种必备工具之一,我们写代码时,经常需要重复完成某些特定的任务,例如编写数据访问代码或者生成自定义集合。我们可以用CodeSmith编写模
板自动完成这些任务,从而不仅提高工作效率,而且能够自动完成那些最为乏味的任务。可惜,CodeSmith是需要注册的,试用版只能用15天。而
MyGeneration基本上和CodeSmith的功能差不多哦,但是他是开源的。我选软件的原则是能开源免费的就用,实在没替代了才选那些需要注册
的,有版权的软件。所以就选MyGeneration了。</p>
<p>
用过一段时间后感觉MyGeneration主要是为了自动生成数据库相关的代码的,可能C#用得比较多,其实我们可以用它生成任何代
码,C++,JavaScript...而且还不仅仅局限于数据库,其他方面的代码也可以用MyGeneration自动生成。比如我们经常用数据访问层
和业务逻辑层,用MyGeneration就可以自动生成这些代码,我们可以不用手动写代码了。比如数据访问层,我们需要调用一个存储过程,用
MyGeneration我们只需要选择生成存储过程代码的模板,执行一下脚本,然后在界面上选择数据库上某个存储过程,然后就自动生成了数据库访问代
码,整个过程只需要点几下鼠标,代码就自动生成了。这对于需要大量操作数据库的程序员来说,效率是多大的提升啊。</p>
<p> 废话少说,还是来点实在的吧。首先声明,我的MyGeneration版本是:1.3.0.3</p>
<p> 安装完MyGeneration后,第一次启动会要求进行一些数据库相关的配置。如图:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/Config.PNG" alt=""></p>
<p> ConnectionString: 就是指定连接哪个数据库了,填好这个就可以点确定了。</p>
<p> 下面来看一看其他的项都是什么。</p>
<p> Language Mapping:就是指定数据库和对象基本类型的映射关系。让我们打开Languages.xml文件看一下吧:</p>
<div class="highlighter"><ol class="highlighter-xml">
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Language</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"SQL"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"C#"</span>
<span class="tag">></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"bigint"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"long"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"binary"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"object"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"bit"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"bool"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"char"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"datetime"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DateTime"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"float"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"double"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"image"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte[]"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"int"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"int"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"money"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"nchar"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"ntext"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"numeric"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"nvarchar"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"real"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"float"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"smalldatetime"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DateTime"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"smallint"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"short"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"smallmoney"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"decimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"text"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"timestamp"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte[]"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"tinyint"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"uniqueidentifier"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"Guid"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"varbinary"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"byte[]"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"varchar"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"xml"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"string"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"sql_variant"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"object"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"></</span>
<span class="tag-name">Language</span>
<span class="tag">></span>
</li>
</ol></div>
<p> 这是里面的一段内容,很明显,是数据库SQL的字段转到C#是什么类型,里面没有C++的,假如我们要让它支持C++的话,需要在这里加入SQL到C++的类型转换。</p>
<p> Database Target Mapping:先看里面的内容吧:</p>
<div class="highlighter"><ol class="highlighter-xml">
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">DbTarget</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"ACCESS"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO"</span>
<span class="tag">></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Text"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbText"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Memo"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbMemo"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"DateTime"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbDate"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Currency"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbCurrency"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Yes/No"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbBoolean"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"OLEObject"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbLongBinary"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Hyperlink"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbMemo"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Double"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbDouble"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"ReplicationID"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbGUID"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Long"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbLong"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Single"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbSingle"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Decimal"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbDecimal"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Byte"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbByte"</span>
<span></span>
<span class="tag">/></span>
</li>
<li>
<span></span>
<span class="tag"><</span>
<span class="tag-name">Type</span>
<span></span>
<span class="attribute">From</span>
<span>=</span>
<span class="attribute-value">"Integer"</span>
<span></span>
<span class="attribute">To</span>
<span>=</span>
<span class="attribute-value">"DAO.dbInteger"</span>
<span></span>
<span class="tag">/></span>
</li>
<li class="alt">
<span></span>
<span class="tag"></</span>
<span class="tag-name">DbTarget</span>
<span class="tag">></span>
</li>
</ol></div>
<p> 呵呵,一目了然,就是Access数据库用DAO的方式访问,数据库的列的类型对应的DAO里是什么类型。</p>
<p> UseMetaData目前没什么用。</p>
<p> 看看MyGeneration的界面吧:</p>
<p> <img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/FrameWork.PNG" alt=""></p>
<p> Template Browser 面板 列出了一些模板,这是自动生成代码需要用到的模板。安装的时候会自己带很多模板。</p>
<p> MyMeta Browser则列出了当前连接的数据库上有些什么库,表,存储过程。</p>
<p> 工作区则是具体模板对应的代码。</p>
<p></p>
<p> 先让我们体验一下吧。</p>
<p> 展开Template Browser面板下"d00dads - C#", 双击 “d00dads - Invoke a Stored Procedure", 让工作区显示其内容,</p>
<p>然后点击工具栏上的 "Execute" 按钮,如图红框所示:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/Exe.PNG" alt=""></p>
<p> 弹出对话框,如图:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080806/CreateSP.PNG" alt=""></p>
<p> 选择数据库,存储过程,存储过程类型,点确定(OK)。</p>
<p></p>
<p> 然后可以看到工作区 Output 里输出了代码了。例如:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System.Collections.Specialized;</span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Data.SqlClient;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">void</span>
<span>dm_exec_cursors(</span>
<span class="keyword">int</span>
<span>spid)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>ListDictionaryparameters=</span>
<span class="keyword">new</span>
<span>ListDictionary();</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>parameters.Add(</span>
<span class="keyword">new</span>
<span>SqlParameter(</span>
<span class="string">"@spid"</span>
<span>,SqlDbType.Int,0),spid);</span>
</li>
<li>
<span>LoadFromSqlNoExec(</span>
<span class="string">"dm_exec_cursors"</span>
<span>,parameters);</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 这就是MyGeneration自动获取了存储过程的输入参数,然后在代码里构造相应的参数,然后生成的代码。</p>
<p> 这只是MyGeneration自带的模板生成的,大家可以试一试其他的模板的效果。</p>
<p> 里面有自动根据表结构生成BLL的类......看下效果:</p>
<div class="highlighter"><ol class="highlighter-c">
<li class="alt">
<span class="comment">/*</span>
</li>
<li>
<span class="comment">'===============================================================================</span>
</li>
<li class="alt">
<span class="comment">'GeneratedFrom-CSharp_dOOdads_BusinessEntity.vbgen</span>
</li>
<li>
<span class="comment">'</span>
</li>
<li class="alt">
<span class="comment">'**IMPORTANT**</span>
</li>
<li>
<span class="comment">'HowtoGenerateyourstoredprocedures:</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'SQL=SQL_StoredProcs.vbgen</span>
</li>
<li class="alt">
<span class="comment">'ACCESS=Access_StoredProcs.vbgen</span>
</li>
<li>
<span class="comment">'ORACLE=Oracle_StoredProcs.vbgen</span>
</li>
<li class="alt">
<span class="comment">'FIREBIRD=FirebirdStoredProcs.vbgen</span>
</li>
<li>
<span class="comment">'POSTGRESQL=PostgreSQL_StoredProcs.vbgen</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'ThesupportingbaseclassOleDbEntityisintheArchitecturedirectoryin"dOOdads".</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'Thisobjectis'abstract'whichmeansyouneedtoinheritfromittobeable</span>
</li>
<li class="alt">
<span class="comment">'toinstantiateit.Thisisveryeasillydone.Youcanoverridepropertiesand</span>
</li>
<li>
<span class="comment">'methodsinyourderivedclass,thisallowsyoutoregeneratethisclassatany</span>
</li>
<li class="alt">
<span class="comment">'timeandnotworryaboutoverwritingcustomcode.</span>
</li>
<li>
<span class="comment">'</span>
</li>
<li class="alt">
<span class="comment">'NEVEREDITTHISFILE.</span>
</li>
<li>
<span class="comment">'</span>
</li>
<li class="alt">
<span class="comment">'publicclassYourObject:_YourObject</span>
</li>
<li>
<span class="comment">'{</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'}</span>
</li>
<li class="alt">
<span class="comment">'</span>
</li>
<li>
<span class="comment">'===============================================================================</span>
</li>
<li class="alt">
<span class="comment">*/</span>
</li>
<li class="alt">
<span class="comment">//GeneratedbyMyGenerationVersion#(1.3.0.3)</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>System;</span>
</li>
<li>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>System.Data.OleDb;</span>
</li>
<li>
<span class="keyword">using</span>
<span>System.Collections;</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>System.Collections.Specialized;</span>
</li>
<li class="alt">
<span class="keyword">using</span>
<span>MyGeneration.dOOdads;</span>
</li>
<li class="alt">
<span class="keyword">namespace</span>
<span>Your.Namespace</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">abstract</span>
<span></span>
<span class="keyword">class</span>
<span>_Users:OleDbEntity</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>_Users()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.QuerySource=</span>
<span class="string">"Users"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.MappingName=</span>
<span class="string">"Users"</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//publicOverridesvoidAddNew()</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>AddNew()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.AddNew();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">string</span>
<span>GetAutoKeyColumn()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="string">"ID"</span>
<span>;</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>FlushData()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>._whereClause=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>._aggregateClause=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.FlushData();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//publicFunctionLoadAll()AsBoolean</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//Loadsalloftherecordsinthedatabase,andsetsthecurrentRowtothefirstrow</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">bool</span>
<span>LoadAll()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ListDictionaryparameters=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.LoadFromSql(</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersLoadAll]"</span>
<span>,parameters);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//publicOverridableFunctionLoadByPrimaryKey()AsBoolean</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//Loadsasinglerowofviatheprimarykey</span>
</li>
<li>
<span></span>
<span class="comment">//=================================================================</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">bool</span>
<span>LoadByPrimaryKey()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ListDictionaryparameters=</span>
<span class="keyword">new</span>
<span>ListDictionary();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.LoadFromSql(</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersLoadByPrimaryKey]"</span>
<span>,parameters);</span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">
<span class="preprocessor"></span>
</li>
<li>
<span class="preprocessor">#regionParameters</span>
</li>
<li class="alt">
<span class="keyword">protected</span>
<span></span>
<span class="keyword">class</span>
<span>Parameters</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">static</span>
<span>OleDbParameterID</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">new</span>
<span>OleDbParameter(</span>
<span class="string">"@ID"</span>
<span>,OleDbType.Integer,0);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">static</span>
<span>OleDbParameterAlias</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">new</span>
<span>OleDbParameter(</span>
<span class="string">"@Alias"</span>
<span>,OleDbType.VarChar,2147483647);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">#endregion<span>
</span>
</li>
<li>
<span class="preprocessor"></span>
</li>
<li class="alt">
<span class="preprocessor">#regionColumnNames</span>
</li>
<li>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>ColumnNames</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>ID=</span>
<span class="string">"ID"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>Alias=</span>
<span class="string">"Alias"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">string</span>
<span>ToPropertyName(</span>
<span class="keyword">string</span>
<span>columnName)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(ht==</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ht=</span>
<span class="keyword">new</span>
<span>Hashtable();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>ht[ID]=_Users.PropertyNames.ID;</span>
</li>
<li>
<span>ht[Alias]=_Users.PropertyNames.Alias;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>(</span>
<span class="keyword">string</span>
<span>)ht[columnName];</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">private</span>
<span>Hashtableht=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li>#endregion<span>
</span>
</li>
<li class="alt">
<span class="preprocessor"></span>
</li>
<li>
<span class="preprocessor">#regionPropertyNames</span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>PropertyNames</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>ID=</span>
<span class="string">"ID"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>Alias=</span>
<span class="string">"Alias"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">string</span>
<span>ToColumnName(</span>
<span class="keyword">string</span>
<span>propertyName)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(ht==</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>ht=</span>
<span class="keyword">new</span>
<span>Hashtable();</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>ht[ID]=_Users.ColumnNames.ID;</span>
</li>
<li class="alt">
<span>ht[Alias]=_Users.ColumnNames.Alias;</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span>(</span>
<span class="keyword">string</span>
<span>)ht[propertyName];</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">static</span>
<span></span>
<span class="keyword">private</span>
<span>Hashtableht=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">#endregion<span>
</span>
</li>
<li class="alt">
<span class="preprocessor">#regionStringPropertyNames</span>
</li>
<li>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>StringPropertyNames</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>ID=</span>
<span class="string">"s_ID"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">const</span>
<span></span>
<span class="keyword">string</span>
<span>Alias=</span>
<span class="string">"s_Alias"</span>
<span>;</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li>#endregion<span>
</span>
</li>
<li class="alt">
<span class="preprocessor"></span>
</li>
<li>
<span class="preprocessor">#regionProperties</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span>IntegerID</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.GetInteger(ColumnNames.ID);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">base</span>
<span>.SetInteger(ColumnNames.ID,value);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span>StringAlias</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">base</span>
<span>.GetString(ColumnNames.Alias);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">base</span>
<span>.SetString(ColumnNames.Alias,value);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}
</span>
</li>
<li class="alt">#endregion<span>
</span>
</li>
<li>
<span class="preprocessor"></span>
</li>
<li class="alt">
<span class="preprocessor">#regionStringProperties</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">string</span>
<span>s_ID</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">get</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">this</span>
<span>.IsColumnNull(ColumnNames.ID)?</span>
<span class="keyword">string</span>
<span>.Empty:</span>
<span class="keyword">base</span>
<span>.GetIntegerAsString(ColumnNames.ID);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">set</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">string</span>
<span>.Empty==value)</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.SetColumnNull(ColumnNames.ID);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">else</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.ID=</span>
<span class="keyword">base</span>
<span>.SetIntegerAsString(ColumnNames.ID,value);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">virtual</span>
<span></span>
<span class="keyword">string</span>
<span>s_Alias</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">return</span>
<span></span>
<span class="keyword">this</span>
<span>.IsColumnNull(ColumnNames.Alias)?</span>
<span class="keyword">string</span>
<span>.Empty:</span>
<span class="keyword">base</span>
<span>.GetStringAsString(ColumnNames.Alias);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">string</span>
<span>.Empty==value)</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.SetColumnNull(ColumnNames.Alias);</span>
</li>
<li>
<span></span>
<span class="keyword">else</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Alias=</span>
<span class="keyword">base</span>
<span>.SetStringAsString(ColumnNames.Alias,value);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li>#endregion</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>AggregateClause_aggregateClause=</span>
<span class="keyword">null</span>
<span>;
</span>
</li>
<li>
<span class="preprocessor">#endregion</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span>IDbCommandGetInsertCommand()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbCommandcmd=</span>
<span class="keyword">new</span>
<span>OleDbCommand();</span>
</li>
<li>
<span>cmd.CommandType=CommandType.StoredProcedure;</span>
</li>
<li class="alt">
<span>cmd.CommandText=</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersInsert]"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>CreateParameters(cmd);</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span>IDbCommandGetUpdateCommand()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbCommandcmd=</span>
<span class="keyword">new</span>
<span>OleDbCommand();</span>
</li>
<li>
<span>cmd.CommandType=CommandType.StoredProcedure;</span>
</li>
<li class="alt">
<span>cmd.CommandText=</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersUpdate]"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>CreateParameters(cmd);</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span>IDbCommandGetDeleteCommand()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbCommandcmd=</span>
<span class="keyword">new</span>
<span>OleDbCommand();</span>
</li>
<li>
<span>cmd.CommandType=CommandType.StoredProcedure;</span>
</li>
<li class="alt">
<span>cmd.CommandText=</span>
<span class="string">"["</span>
<span>+</span>
<span class="keyword">this</span>
<span>.SchemaStoredProcedure+</span>
<span class="string">"proc_UsersDelete]"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>OleDbParameterp;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>IDbCommandCreateParameters(OleDbCommandcmd)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>OleDbParameterp;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>p=cmd.Parameters.Add(Parameters.ID);</span>
</li>
<li class="alt">
<span>p.SourceColumn=ColumnNames.ID;</span>
</li>
<li>
<span>p.SourceVersion=DataRowVersion.Current;</span>
</li>
<li>
<span>p=cmd.Parameters.Add(Parameters.Alias);</span>
</li>
<li class="alt">
<span>p.SourceColumn=ColumnNames.Alias;</span>
</li>
<li>
<span>p.SourceVersion=DataRowVersion.Current;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">return</span>
<span>cmd;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 这就是自动获得表结构,然后从字段映射成类里面的成员,并且还有一些插入,更新,删除的代码。</p>
<p> 当然自带的模板生成的代码不一定符合我们的需要,但是我们可以根据需要自己写一些模板来生成符合自己需要的代码,这也是非常容易的事,欲知如何实现,请看下回文章。</p>
<p>===========================================================</p>
<p>要用MyGeneration就必须要和各种模板打交道。我们可以使用别人写的模板来完成我们说需要的功能,但是别人写的模板不一定最适合我们的项目里面的代码,所以有时也需要自己写一些模板来使用。下面就讲如何编写模板吧<br>
通过File – New 菜单我们可以看到,MyGeneration支持的模板包括C#,VB.Net,Jscript,VBScript,我们可以选则自己擅长的语言来写模板。<br>
最简单的办法就是找一个功能差不多的模板,然后在这个模板的基础上进行修改了。这个也是初学的办法,可以快速的了解如何实现特定的功能。<br><br>
当然,我们要自己建一个模板,以C#模板为例吧。<br>
假如我们要自己生成一个数据库的数据表所对应的BLL类。<br>
在UI界面上需要我们选择是哪个数据库,那张数据表。最后生成BLL类。<br><br>
选择菜单 File – New – C# Zeus
Template,就打开了一个新的工作区,该模板的代码是C#的。工作区有5个Tab页,Template Code, Interface
Code, Template Source, Interface Source, Output.如下图所示: </p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080903/MyGeneration1.PNG" alt=""></p>
<p> 这些都干什么的呢?<br>
这得了解一下MyGeneration内部的结构了。MyGeneration通过脚本引擎执行Template Code和Interface
Code中的脚本,生成Template Source和Interface Source的代码,然后执行Interface
Source显示用户界面,再执行Template Source,输出结果到Output中。<br>
由此可见,Template
Source是根据Template code生成的Interface Source是根据Interface
code生成的,他们是不可编辑的。我们能用代码控制的就是Template code和Interface code了。而Interface
code是主要和用户界面有关的,Template code则主要控制执行完用户界面的代码后如何输出到Output。<br><br>
默认生成的Interface Code如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedGui:DotNetScriptGui</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span>GeneratedGui(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li>
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//TheUserInterfaceEntryPoint</span>
</li>
<li>
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Setup()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//**UNCOMMENTCODEBELOWTOSEEUI**</span>
</li>
<li>
<span></span>
<span class="comment">//ui.Width=100;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.Height=100;</span>
</li>
<li>
<span></span>
<span class="comment">//GuiLabellblDemo=ui.AddLabel("lblDemo","Demo","DemoTooltip");</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.ShowGui=true;</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p><br>
Interface Code中编写用户界面有两种方法,一种是采用MyGeneration提供的GUI库,另外一种则是完全采用C#本身的界面代码编写。下图显示了MyGeneration的GUI库的帮助,从中可以看到其提供的各种GUI类。</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080903/MyGenerationGUI.PNG" alt=""><br></p>
<p> 我可不想为了写个模板去学习使用MyGeneration的GUI库,这可加大了学习成本,而且所有界面都得用代码一行一行的写出来,多麻烦啊。如果能像C#那样,直接用鼠标拖拽控件就能搞定界面UI就好了。<br>
其
实,我们是可以用C#的界面库的。我的方法是这样的。先自己用VS新建一个Windows的工程,然后在窗体上摆好控件,设置各种控件的属性,并且把需要
用的各种控件的事件都让VS的IDE生成好。然后把这个窗体的代码直接拷贝到MyGeneration的Interface
code里面去。注意,假设我们的窗体叫Form1,我们需要拷贝Form1.Designer.cs 和Form1.cs两个文件中的代码。<br><br>
然后在Interface Code最前面加入下面两行:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%#REFERENCESystem.Windows.Forms.dll,System.Drawing.dll%></span>
</li>
<li class="alt">
<span><%#NAMESPACESystem.Windows.Forms,System.Drawing%></span>
</li>
</ol></div>
<p><br>
在Setup()函数中写:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span>Form1form=</span>
<span class="keyword">new</span>
<span>Form1();</span>
</li>
<li class="alt">
<span class="keyword">if</span>
<span>(form.ShowDialog()!=DialogResult.OK)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ui.IsCanceled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p></p>
<p> 最后所形成的Interface Code的代码如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%#REFERENCESystem.Windows.Forms.dll,System.Drawing.dll%></span>
</li>
<li class="alt">
<span><%#NAMESPACESystem.Windows.Forms,System.Drawing%></span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedGui:DotNetScriptGui</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedGui(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="comment">//TheUserInterfaceEntryPoint</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Setup()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//**UNCOMMENTCODEBELOWTOSEEUI**</span>
</li>
<li>
<span></span>
<span class="comment">//ui.Width=100;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.Height=100;</span>
</li>
<li>
<span></span>
<span class="comment">//GuiLabellblDemo=ui.AddLabel("lblDemo","Demo","DemoTooltip");</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.ShowGui=true;</span>
</li>
<li>
<span>Form1form=</span>
<span class="keyword">new</span>
<span>Form1();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(form.ShowDialog()!=DialogResult.OK)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ui.IsCanceled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>Form1:Form</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///Requireddesignervariable.</span>
</li>
<li>
<span></span>
<span class="comment">///</summary></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.ComponentModel.IContainercomponents=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///<summary></span>
</li>
<li>
<span></span>
<span class="comment">///Cleanupanyresourcesbeingused.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="comment">///<paramname="disposing">trueifmanagedresourcesshouldbedisposed;otherwise,false.</param></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Dispose(</span>
<span class="keyword">bool</span>
<span>disposing)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(disposing&&(components!=</span>
<span class="keyword">null</span>
<span>))</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>components.Dispose();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.Dispose(disposing);</span>
</li>
<li>
<span>}
</span>
</li>
<li>#regionWindowsFormDesignergeneratedcode</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///RequiredmethodforDesignersupport-donotmodify</span>
</li>
<li>
<span></span>
<span class="comment">///thecontentsofthismethodwiththecodeeditor.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>InitializeComponent()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ComboBox();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ListBox();</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.Button();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.SuspendLayout();</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//comboBox1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,24);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Name=</span>
<span class="string">"comboBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,20);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.TabIndex=0;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndexChanged+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.comboBox1_SelectedIndexChanged);</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//listBox1</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.ItemHeight=12;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,50);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Name=</span>
<span class="string">"listBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,196);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.TabIndex=1;</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//button1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(180,252);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.Name=</span>
<span class="string">"button1"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(75,23);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.TabIndex=2;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Text=</span>
<span class="string">"OK"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.UseVisualStyleBackColor=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//Form1</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleDimensions=</span>
<span class="keyword">new</span>
<span>System.Drawing.SizeF(6F,12F);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.ClientSize=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(284,293);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.button1);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.listBox1);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.comboBox1);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.MaximizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.MinimizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Name=</span>
<span class="string">"Form1"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Text=</span>
<span class="string">"ZhouKai'sBLLClass"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Load+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.Form1_Load);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.ResumeLayout(</span>
<span class="keyword">false</span>
<span>);</span>
</li>
<li class="alt">
<span>}
</span>
</li>
<li class="alt">#endregion</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ComboBoxcomboBox1;</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ListBoxlistBox1;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.Buttonbutton1;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span>Form1()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>InitializeComponent();</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>comboBox1_SelectedIndexChanged(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li>
<span>{</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>Form1_Load(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 执行一下该模板,看有什么效果吧?是不是看到了如下的UI呢?</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080903/NoDataUI.PNG" alt=""></p>
<p> 呵呵,这个只有UI,还没有数据,下次该讲如何给这个UI加上他说需要的数据啦。比如如何获得数据库的各个数据表啊之内的,然后如何输出自己说需要BLL代码和存储过程的代码啦。</p>
<p> 呵呵,发现竟然有网站转载我的文章了,心里小小高兴一下,说明写的东西还是有点用的。</p>
<p>==============================================================</p>
<p>前面讲了MyGeneration的使用,以及如何自己写模板显示UI,现在开始讲如何将数据库的信息显示到UI上。<br>
在MyGeneraion脚本系统中,有一个全局变量,叫做MyMeta,他是dbRoot类型的。通过这个全局变量,我们可以获得数据库相关的信息。这个变量在Interface Code 和Template Code中都可以使用。<br>
从上节代码来看,我们建立Form窗口的时候,就把这个变量给传给了Form窗口<br>
如下代码所示:<br>
MyForm form = new MyForm(MyMeta, input);<br>
那么dbRoot类型的MyMeta变量都有哪些功能,有哪些函数,属性可以使用呢?<br>
我们可以查找帮助,通过MyGeneration菜单 “File – Help – MyMeta API Reference”可以打开其帮助,里面有dbRoot的详细介绍,如下图所示:</p>
<p><img src="https://p-blog.csdn.net/images/p_blog_csdn_net/zxcred/EntryImages/20080910/dbRoot.PNG" alt=""><br><br>
大概看一下API,然后我们就可以修改我们的模板了,将其修改如下:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>Form1_Load(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//获取数据库,传给ComboBox</span>
</li>
<li class="alt">
<span>comboBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.Databases;</span>
</li>
<li>
<span></span>
<span class="comment">//ComboBox显示数据库名</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase!=</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//选中默认的数据库</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndex=</span>
<span class="keyword">this</span>
<span>.comboBox1.FindStringExact(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Name);</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//通过IDatabase的Tables属性取得数据库里面所有的数据表</span>
</li>
<li>
<span></span>
<span class="comment">//作为数据源传给ListBox</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Tables;</span>
</li>
<li>
<span></span>
<span class="comment">//ListBox显示数据表的名字</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span></span>
</li>
</ol></div>
<p> 就算没有注释,这代码也很好懂吧。<br>
呵呵,由此可见,我们可以通过MyMeta得到IDataBases,然后获得ITable,在通过ITable<br>
获得数据表的信息……反正呢,照这条线找下去,基本上数据库里有哪些表,表里有哪些字段,有哪些存储过程,任何信息都可以很容易的得到。当然了,在自己写模板之前,最好先大概看一下“MyMeta API Reference”,这样就写模板就会更得心应手了。<br>
下面接下来看Form点击OK之后的代码吧</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>button1_Click(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>IDatabasedatabase=</span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>IDatabase;</span>
</li>
<li class="alt">
<span>ITabletable=</span>
<span class="keyword">this</span>
<span>.listBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>ITable;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"databaseName"</span>
<span>]=database.Name;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"tableName"</span>
<span>]=table.Name;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.DialogResult=DialogResult.OK;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Close();</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p>
这段代码得重点讲一下了,因为Interface Code是关于UI的,那么UI执行完了之后,我们就需要用到Template
Code了,那么Template Code和Interface Code是如何联系在一起的呢? 我们在Template
Code里面如何知道Interface Code显示的UI中用户输入了什么,选择了什么?<br>
这用到了MyGeneration的一个非常重要的类IZeusInput。<br>
改接口的详细信息请看帮助 “File – help – Zeus API Reference”。<br>
在这里我们简单的把UI上选中的数据库的名字和数据表的名字存在了IZeusInput变量中。大家注意看下Form的IZeumInput是如何来的吧。<br>
这样,我们就完成了该模板的Interface Code的代码,最后形成的Interface Code就如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%#REFERENCESystem.Windows.Forms.dll,System.Drawing.dll%></span>
</li>
<li class="alt">
<span><%#NAMESPACESystem.Windows.Forms,System.Drawing%></span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedGui:DotNetScriptGui</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedGui(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="comment">//TheUserInterfaceEntryPoint</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//-----------------------------------------</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Setup()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//**UNCOMMENTCODEBELOWTOSEEUI**</span>
</li>
<li>
<span></span>
<span class="comment">//ui.Width=100;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.Height=100;</span>
</li>
<li>
<span></span>
<span class="comment">//GuiLabellblDemo=ui.AddLabel("lblDemo","Demo","DemoTooltip");</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ui.ShowGui=true;</span>
</li>
<li>
<span>Form1form=</span>
<span class="keyword">new</span>
<span>Form1(MyMeta,input);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(form.ShowDialog()!=DialogResult.OK)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>ui.IsCanceled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>Form1:Form</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///Requireddesignervariable.</span>
</li>
<li>
<span></span>
<span class="comment">///</summary></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.ComponentModel.IContainercomponents=</span>
<span class="keyword">null</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///<summary></span>
</li>
<li>
<span></span>
<span class="comment">///Cleanupanyresourcesbeingused.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="comment">///<paramname="disposing">trueifmanagedresourcesshouldbedisposed;otherwise,false.</param></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">protected</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Dispose(</span>
<span class="keyword">bool</span>
<span>disposing)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">if</span>
<span>(disposing&&(components!=</span>
<span class="keyword">null</span>
<span>))</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>components.Dispose();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">base</span>
<span>.Dispose(disposing);</span>
</li>
<li>
<span>}
</span>
</li>
<li>#regionWindowsFormDesignergeneratedcode</li>
<li>
<span></span>
<span class="comment">///<summary></span>
</li>
<li class="alt">
<span></span>
<span class="comment">///RequiredmethodforDesignersupport-donotmodify</span>
</li>
<li>
<span></span>
<span class="comment">///thecontentsofthismethodwiththecodeeditor.</span>
</li>
<li class="alt">
<span></span>
<span class="comment">///</summary></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>InitializeComponent()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ComboBox();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.ListBox();</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1=</span>
<span class="keyword">new</span>
<span>System.Windows.Forms.Button();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.SuspendLayout();</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//comboBox1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,24);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Name=</span>
<span class="string">"comboBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,20);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.TabIndex=0;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndexChanged+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.comboBox1_SelectedIndexChanged);</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="comment">//listBox1</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.FormattingEnabled=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.ItemHeight=12;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(22,50);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Name=</span>
<span class="string">"listBox1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(233,196);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.TabIndex=1;</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//button1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Location=</span>
<span class="keyword">new</span>
<span>System.Drawing.Point(180,252);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.Name=</span>
<span class="string">"button1"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Size=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(75,23);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.TabIndex=2;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Text=</span>
<span class="string">"OK"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.button1.UseVisualStyleBackColor=</span>
<span class="keyword">true</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.button1.Click+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.button1_Click);</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//Form1</span>
</li>
<li>
<span></span>
<span class="comment">//</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleDimensions=</span>
<span class="keyword">new</span>
<span>System.Drawing.SizeF(6F,12F);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.ClientSize=</span>
<span class="keyword">new</span>
<span>System.Drawing.Size(284,293);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.button1);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.listBox1);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Controls.Add(</span>
<span class="keyword">this</span>
<span>.comboBox1);</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.MaximizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.MinimizeBox=</span>
<span class="keyword">false</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Name=</span>
<span class="string">"Form1"</span>
<span>;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.Text=</span>
<span class="string">"ZhouKai'sBLLClass"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Load+=</span>
<span class="keyword">new</span>
<span>System.EventHandler(</span>
<span class="keyword">this</span>
<span>.Form1_Load);</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.ResumeLayout(</span>
<span class="keyword">false</span>
<span>);</span>
</li>
<li>
<span>}
</span>
</li>
<li>#endregion</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ComboBoxcomboBox1;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.ListBoxlistBox1;</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>System.Windows.Forms.Buttonbutton1;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span>dbRootmyMeta;</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span>IZeusInputzeusInput;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>Form1(dbRootmyMeta,IZeusInputzeusInput)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.myMeta=myMeta;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.zeusInput=zeusInput;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span>InitializeComponent();</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>comboBox1_SelectedIndexChanged(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span>IDatabasedatabase=</span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>IDatabase;</span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(database!=</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DataSource=database.Tables;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>Form1_Load(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//获取数据库,传给ComboBox</span>
</li>
<li>
<span>comboBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.Databases;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ComboBox显示数据库名</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">if</span>
<span>(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase!=</span>
<span class="keyword">null</span>
<span>)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="comment">//选中默认的数据库</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedIndex=</span>
<span class="keyword">this</span>
<span>.comboBox1.FindStringExact(</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Name);</span>
</li>
<li>
<span></span>
<span class="comment">//通过IDatabase的Tables属性取得数据库里面所有的数据表</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//作为数据源传给ListBox</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DataSource=</span>
<span class="keyword">this</span>
<span>.myMeta.DefaultDatabase.Tables;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//ListBox显示数据表的名字</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.listBox1.DisplayMember=</span>
<span class="string">"Name"</span>
<span>;</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">void</span>
<span>button1_Click(</span>
<span class="keyword">object</span>
<span>sender,EventArgse)</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>IDatabasedatabase=</span>
<span class="keyword">this</span>
<span>.comboBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>IDatabase;</span>
</li>
<li class="alt">
<span>ITabletable=</span>
<span class="keyword">this</span>
<span>.listBox1.SelectedValue</span>
<span class="keyword">as</span>
<span>ITable;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"databaseName"</span>
<span>]=database.Name;</span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.zeusInput[</span>
<span class="string">"tableName"</span>
<span>]=table.Name;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">this</span>
<span>.DialogResult=DialogResult.OK;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">this</span>
<span>.Close();</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
</ol></div>
<p> 下面来看Template Code如何写吧。目前的模板默认的Template Code如下所示:<br></p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%</span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedTemplate:DotNetScriptTemplate</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedTemplate(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//---------------------------------------------------</span>
</li>
<li>
<span></span>
<span class="comment">//Render()iswhereyouwanttowriteyourlogic</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//---------------------------------------------------</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Render()</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span>%></span>
</li>
<li class="alt">
<span>Youcantoggle</span>
<span class="keyword">in</span>
<span></span>
<span class="keyword">out</span>
<span>ofscriptlike</span>
<span class="keyword">this</span>
</li>
<li>
<span><%</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"Helloworld."</span>
<span>);</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>%></span>
</li>
</ol></div>
<p> C# Template Code的语法和Asp的语法十分的类似<%=%>表示绑定某一个字段或属性<br>
<%%>表示脚本段,我们可以在这里写符合C#语法的任何语句<br>
其他的内容不进行解析直接用output写到最后的结果里<br><br>
修改Template Code,改成如下所示:</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span><%</span>
</li>
<li class="alt">
<span class="keyword">public</span>
<span></span>
<span class="keyword">class</span>
<span>GeneratedTemplate:DotNetScriptTemplate</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>GeneratedTemplate(ZeusContextcontext):</span>
<span class="keyword">base</span>
<span>(context){}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">override</span>
<span></span>
<span class="keyword">void</span>
<span>Render()</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//设置输出语言...</span>
</li>
<li>
<span>MyMeta.Language=</span>
<span class="string">"C#"</span>
<span>;</span>
</li>
<li class="alt">
<span>MyMeta.DbTarget=</span>
<span class="string">"SQLClient"</span>
<span>;</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//获得执行完UI后,我们说选中的数据库名,数据表名</span>
</li>
<li>
<span></span>
<span class="keyword">string</span>
<span>databaseName=input[</span>
<span class="string">"databaseName"</span>
<span>].ToString();</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">string</span>
<span>tableName=input[</span>
<span class="string">"tableName"</span>
<span>].ToString();</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>IDatabasedatabase=MyMeta.Databases[databaseName];</span>
</li>
<li>
<span></span>
<span class="comment">//获得数据表的接口</span>
</li>
<li class="alt">
<span>ITabletable=database.Tables[tableName];</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span>%></span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span class="keyword">namespace</span>
<span>BLL</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System;</span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System.Collections;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System.Collections.Generic;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span><%</span>
</li>
<li>
<span></span>
<span class="comment">//数据表的名称作为BLL类的类名</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"publicpartialclass"</span>
<span>+tableName);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"{"</span>
<span>);</span>
</li>
<li class="alt">
<span></span>
</li>
<li>
<span></span>
<span class="comment">//遍历数据表中所有字段</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">foreach</span>
<span>(IColumncolumn</span>
<span class="keyword">in</span>
<span>table.Columns)</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="comment">//将字段名第一个字母转为小写</span>
</li>
<li>
<span></span>
<span class="keyword">string</span>
<span>tmpColumnName=DnpUtils.SetCamelCase(column.Name);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"private"</span>
<span>+column.LanguageType+</span>
<span class="string">"_"</span>
<span>+tmpColumnName+</span>
<span class="string">";"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"public"</span>
<span>+column.LanguageType+</span>
<span class="string">""</span>
<span>+column.Name);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"{"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"get{return_"</span>
<span>+tmpColumnName+</span>
<span class="string">";}"</span>
<span>);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"set{_"</span>
<span>+tmpColumnName+</span>
<span class="string">"=value;}"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"}"</span>
<span>);</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">""</span>
<span>);</span>
</li>
<li>
<span>}</span>
</li>
<li class="alt">
<span>output.writeln(</span>
<span class="string">"}"</span>
<span>);</span>
</li>
<li>
<span>output.writeln(</span>
<span class="string">"}"</span>
<span>);</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span>%></span>
</li>
</ol></div>
<p> 这代码也非常简单,基本不用解释都能看懂,不用几分钟就能自己写出来了。<br>
运行一下该模板,看下是不是类似有如下输出呢?</p>
<div class="highlighter"><ol class="highlighter-c">
<li>
<span class="keyword">namespace</span>
<span>BLL</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">using</span>
<span>System;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">using</span>
<span>System.Data;</span>
</li>
<li>
<span></span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span>partial</span>
<span class="keyword">class</span>
<span>User</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">int</span>
<span>_id;</span>
</li>
<li>
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">int</span>
<span>ID</span>
</li>
<li class="alt">
<span>{</span>
</li>
<li>
<span></span>
<span class="keyword">get</span>
<span>{</span>
<span class="keyword">return</span>
<span>_id;}</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">set</span>
<span>{_id=value;}</span>
</li>
<li>
<span>}</span>
</li>
<li>
<span></span>
<span class="keyword">private</span>
<span></span>
<span class="keyword">string</span>
<span>_userName;</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">public</span>
<span></span>
<span class="keyword">string</span>
<span>UserName</span>
</li>
<li>
<span>{</span>
</li>
<li class="alt">
<span></span>
<span class="keyword">get</span>
<span>{</span>
<span class="keyword">return</span>
<span>_userName;}</span>
</li>
<li>
<span></span>
<span class="keyword">set</span>
<span>{_userName=value;}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li class="alt">
<span>}</span>
</li>
<li>
<span>}</span>
</li>
</ol></div>
<p> 好了,最简单的一个生成BLL类的模板就完成了。<br>
本来还想写下如何用MyGeneration来生成执行存储过程的函数,如何用MyGeneration来生成测试存储过程函数的代码的。我觉得完成了这个模板,基本上就可以自己查找MyGeneration的帮助中的API完成这些了,应该不用继续下去了。<br>
继续意淫一下,假如写数据库相关的程序,有了MyGeneration,我们的工作就简单多了,我们可以自己写模板,自动生成BLL类,DAL代码,自动生成测试代码……多棒啊</p>
<p></p>
<p>本文来自<br><a style="font-size: 15pt;" href="http://blog.csdn.net/zxcred">http://blog.csdn.net/zxcred</a>
</p>
<p></p>
<p>MyGeneration下载地址</p>
<p>http://download.csdn.net/source/1328655</p>
<p></p>