Fork me on GitHub

使用T4模板技术


1、右键->添加->新建项,选择“文本模板”

2、修改代码为:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>

using System;
<#@ assembly name="System.Data" #>
<#@ assembly name="System.xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Data" #>

namespace Acmeway.IDLL 
{     

      <#

           string connectionString = "server=192.168.2.74;database=TJSHMPTest;uid=sa;pwd=sa"; 
           SqlConnection conn = new SqlConnection(connectionString); 
           conn.Open();
           System.Data.DataTable schema = conn.GetSchema("TABLES");
           string selectQuery = "select * from @tableName"; 

           SqlCommand   command = new SqlCommand (selectQuery,conn); 

           SqlDataAdapter ad = new SqlDataAdapter(command); 

           System.Data.DataSet ds = new DataSet();         

           foreach(System.Data.DataRow row in schema.Rows) 
           {  #>   

           public class <#= row["TABLE_NAME"].ToString().Trim('s') #> 
           {    <#                      

                   ds.Tables.Clear();

                  command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString()); 

                  ad.FillSchema(ds, SchemaType.Mapped, row["TABLE_NAME"].ToString());          

                  foreach (DataColumn dc in ds.Tables[0].Columns)
                  {    #>                     

                  private <#= dc.DataType.Name #> _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower())      #>;                       

                  public <#= dc.DataType.Name #> <#= dc.ColumnName #>
                  {

                     get { return _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #>; } 

                     set { _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #> = value; }

                  }                                                 

              <# }  #>          

           }                   

           <# 

           } #>                 

}
posted @ 2013-08-08 15:53  迁梦余光  阅读(258)  评论(0编辑  收藏  举报