<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Core" #> <#@ Assembly Name="System.Windows.Forms" #> <#@ Assembly Name="$(ProjectDir)\MySql.Data.dll" #> <#@ Assembly Name="System.Data" #> <#@ Assembly Name="System" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="MySql.Data" #> <#@ import namespace="MySql.Data.MySqlClient" #> <# string Db_Name="repairsystem",Table_Name="stock_infolist"; string conStr="server=localhost;database=repairsystem;uid=root;pwd=root"; MySqlConnection con=new MySqlConnection(conStr); con.Open(); MySqlParameter mp1=new MySqlParameter("@db_name",Db_Name); MySqlParameter mp2=new MySqlParameter("@table_name",Table_Name); string sqlStr="select * from information_schema.COLUMNS where [email protected]_name ";//and [email protected]_name"; MySqlCommand cmd=new MySqlCommand(sqlStr,con); cmd.Parameters.Add(mp1); cmd.Parameters.Add(mp2); MySqlDataReader dr = cmd.ExecuteReader(); #> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data;
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".cs" #> <#@ include file="Base.tt" #> <# var TableList=new List<string>(); var DetailList=new List<Tuple<string,string,string>>(); var TableName=""; while(dr.Read()) { var Table=dr["TABLE_NAME"].ToString(); if(TableName==Table) { var Column=dr["COLUMN_NAME"].ToString(); var DataTypeOld=dr["DATA_TYPE"].ToString(); var DataType=DataTypeOld=="varchar"?"string": (DataTypeOld=="int"?"int": (DataTypeOld=="datetime"?"DateTime": (DataTypeOld=="decimal"?"decimal": (DataTypeOld=="char"?"string": (DataTypeOld=="bit"?"Boolean":"string"))))); DetailList.Add(Tuple.Create(Table,DataType,Column)); } else { TableList.Add(Table); } TableName=Table; } #> namespace DataBaseEntity { <# foreach(var item in TableList) { #> public class <#= item #> {<# foreach(var detail in DetailList) {if( item == detail.Item1 ) { #> public <#=detail.Item2#> <#=detail.Item3#> { get; set; } <#}}#> } <# } #>}
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".cs" #> <#@ include file="Base.tt" #> <# var TableInfo=""; List<Tuple<string,string>> list=new List<Tuple<string,string>>(); while(dr.Read()) { var TableName=dr["table_name"].ToString(); if(TableName==TableInfo) { var Column=dr["column_name"].ToString(); var DataType=dr["data_type"].ToString(); switch(DataType) { case "varchar":DataType="string"; break; case "int":DataType="int"; break; case "datetime":DataType="DateTime"; break; case "decimal":DataType="decimal"; break; case "char":DataType="string"; break; case "double":DataType="double"; break; case "float":DataType="float"; break; case "bit":DataType="Boolean"; break; default:DataType="string"; break; } list.Add(Tuple.Create(Column,DataType)); } TableInfo=TableName; } #> // This is the output code from your template // you only get syntax-highlighting here - not intellisense using System; using System.Collections.Generic; using System.Linq; using System.Text; using Dapper; using System.Data; namespace MyNameSpace { class <#= TableInfo #> { <# foreach(var item in list) { #> public <#= item.Item2 #> <#= item.Item1 #> { get ; set ; } <# } #> } }
时间: 2024-10-07 02:59:03