CodeSimth生成数据库建表语句

添加一个类型为SchemaExplorer.TableSchemaCollection的输入参数。

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchemaCollection" Category="Context" Description="Tables to get the Sql." %>

在模板正文中加入下面的代码:


<%for(int j=0;j<SourceTable.Count;j++){%>
create table 
<%=SourceTable[j].Name%>
(
<% for(int i=0;i<SourceTable[j].Columns.Count;i++) {%>
    
<%=SourceTable[j].Columns[i].Name%> <%=SourceTable[j].Columns[i].NativeType%>(<%=SourceTable[j].Columns[i].Size%>)<% if(i!=SourceTable[j].Columns.Count-1){%>,<%}%>
<%}%>
);
<%}
不需要自己另外写方法了,SchemaExplorer.TableSchemaCollection对象已经包含了我们创建表所需要的信息,

字段名,字段类型,字段长度等,自己拼接一下就好了。

So easy!

原文地址:https://www.cnblogs.com/juan/p/1424359.html