TmpQuery.cs using Da.Dao; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace Da.Extend { public class TmpQuery { private string _connStr = string.Empty; private string _dbName = string.Empty; public TmpQuery(string connStr, string dbName) { _connStr = connStr; _dbName = dbName; } public IList<string> GetTables() { IList<string> list=null; string sqlStr = "select name from sysobjects where type='u'"; DataSet ds=DbHelper.GetQuery(_connStr,sqlStr); if(ds==null) { return list; } if(ds.Tables.Count<=0) { return list; } list=new List<string>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { string tableName = ds.Tables[0].Rows[i]["name"].ToString(); list.Add(tableName); } return list; } public IList<DbColumnEntity> GetColumns(string tableName) { IList<DbColumnEntity> list = null; StringBuilder sqlStr = new StringBuilder(); sqlStr.Append("SELECT a.name TabLeName,b.name ColumnName,c.DATA_TYPE ColumnType,b.max_length ColumnLen "); sqlStr.Append("FROM sys.tables a join sys.columns b on b.object_id = a.object_id "); sqlStr.Append("join INFORMATION_SCHEMA.COLUMNS c on b.name=c.COLUMN_NAME and a.name=c.TABLE_NAME "); sqlStr.AppendFormat("where a.name='{0}'", tableName); DataSet ds = DbHelper.GetQuery(_connStr, sqlStr.ToString()); if (ds == null) { return list; } if (ds.Tables.Count <= 0) { return list; } list = new List<DbColumnEntity>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DbColumnEntity column = new DbColumnEntity(); column.ColumnName =Convert.ToString(ds.Tables[0].Rows[i]["ColumnName"]); column.ColumnType = Convert.ToString(ds.Tables[0].Rows[i]["ColumnType"]); column.ColumnLen = Convert.ToInt32(ds.Tables[0].Rows[i]["ColumnLen"]); list.Add(column); } return list; } } }
ModeAutoGenterTemplate.tt <#@ template language="C#" debug="True" hostspecific="True" #> <#@ output extension=".cs" #> <#@ assembly name="Microsoft.CSharp" #> <#@ assembly name="System" #> <#@ assembly name="System.Core" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.Data.DataSetExtensions" #> <#@ assembly name="System.Xml" #> <#@ assembly name="System.Xml.Linq" #> <#@ assembly name="$(TargetDir)\Da.Dao.dll" #> <#@ assembly name="$(TargetDir)\Da.Extend.dll" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="Da.Dao" #> <#@ import namespace="Da.Extend" #> <#@ include file="ManagerAuto.ttinclude"#> <# var manager = new Manager(Host, GenerationEnvironment, true) { OutputPath = Path.GetDirectoryName(Host.TemplateFile)}; var query = new TmpQuery("Data Source=172.17.51.67;Initial Catalog=master;User ID=sa;Password=kingdee$2015;", "master"); var tableList = query.GetTables(); #> <# foreach(string talbeName in tableList){ #> <# manager.StartBlock(talbeName+".cs"); #> using System; namespace MyProject.Entities { [Serializable()] public class <#= talbeName #> { <# IList<DbColumnEntity> list=query. GetColumns(talbeName); if(list!=null&&list.Count>0) { foreach(DbColumnEntity dbColumn in list){ #> public string <#=dbColumn.ColumnName#> {get;set;} <# } #> <# } #> } } <# manager.EndBlock(); #> <# } #> <# manager.Process(true); #>