首页 > 代码库 > T4自动生成数据库C#实体类学习(1)

T4自动生成数据库C#实体类学习(1)

第一个测试的Demo

 1 <#@ template language="C#" debug="True" hostspecific="True" #> 2 <#@ output extension=".cs" #> 3 <#@ assembly name="System.Data" #> 4  5 <#@ assembly name="System.xml" #> 6 <#@ assembly name="System.Data.OracleClient" #> 7 <#@ import namespace="System.Collections.Generic" #> 8 <#@ import namespace="System.Data.OracleClient" #> 9 <#@ import namespace="System.Data" #>10 11 using System;12 using System.Xml;13 using System.Data;14 using System.Data.OracleClient;15 16 namespace MyProject.Entities 17 {    18     <#19         string connectionString = "Data Source=orcl;User ID=sde;PassWord=sde"; 20         OracleConnection conn = new OracleConnection(connectionString); 21         conn.Open(); 22         string tableName= "T_USER";23         string selectQuery = "select * from @tableName"; 24         OracleCommand command = new OracleCommand(selectQuery,conn);     25         System.Data.DataSet ds = new DataSet();             26     #> 27             28         public class <#=  tableName#>                    29         {30             <#                                     31                 ds.Tables.Clear();32                 OracleDataAdapter ad=new OracleDataAdapter(command);33                 command.CommandText = selectQuery.Replace("@tableName", tableName); 34                      35                 ad.FillSchema(ds, SchemaType.Mapped, tableName);36                     37                 foreach (DataColumn dc in ds.Tables[0].Columns)38                 {39             #>40                         41                 private <#= dc.DataType.Name  #> _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower())      #>;42                     43                     44                 public <#= dc.DataType.Name #> <#= dc.ColumnName #> 45                 {46                     get { return _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #>; } 47                     set { _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #> = value; }48                 }                    49                                 50             <#    51                 }                 52             #>53                 54                 55             }                56             57             <#    58              #>            59 }

 

T4自动生成数据库C#实体类学习(1)