http://www.cnblogs.com/zengxiangzhan/p/3250105.html

Manager.ttinclude

<#@ assembly name="System.Core"#>
<#@ assembly name="System.Data.Linq"#>
<#@ assembly name="EnvDTE"#>
<#@ assembly name="System.Xml"#>
<#@ assembly name="System.Xml.Linq"#>
<#@ import namespace="System"#>
<#@ import namespace="System.CodeDom"#>
<#@ import namespace="System.CodeDom.Compiler"#>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Data.Linq"#>
<#@ import namespace="System.Data.Linq.Mapping"#>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Reflection"#>
<#@ import namespace="System.Text"#>
<#@ import namespace="System.Xml.Linq"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#+ // Manager class records the various blocks so it can split them up
class Manager {
private class Block {
public String Name;
public int Start, Length;
} private Block currentBlock;
private List<Block> files = new List<Block>();
private Block footer = new Block();
private Block header = new Block();
private ITextTemplatingEngineHost host;
private StringBuilder template;
protected List<String> generatedFileNames = new List<String>(); public static Manager Create(ITextTemplatingEngineHost host, StringBuilder template) {
return (host is IServiceProvider) ? new VSManager(host, template) : new Manager(host, template);
} public void StartNewFile(String name) {
if (name == null)
throw new ArgumentNullException("name");
CurrentBlock = new Block { Name = name };
} public void StartFooter() {
CurrentBlock = footer;
} public void StartHeader() {
CurrentBlock = header;
} public void EndBlock() {
if (CurrentBlock == null)
return;
CurrentBlock.Length = template.Length - CurrentBlock.Start;
if (CurrentBlock != header && CurrentBlock != footer)
files.Add(CurrentBlock);
currentBlock = null;
} public virtual void Process(bool split) {
if (split) {
EndBlock();
String headerText = template.ToString(header.Start, header.Length);
String footerText = template.ToString(footer.Start, footer.Length);
String outputPath = Path.GetDirectoryName(host.TemplateFile);
files.Reverse();
foreach(Block block in files) {
String fileName = Path.Combine(outputPath, block.Name);
String content = headerText + template.ToString(block.Start, block.Length) + footerText;
generatedFileNames.Add(fileName);
CreateFile(fileName, content);
template.Remove(block.Start, block.Length);
}
}
} protected virtual void CreateFile(String fileName, String content) {
if (IsFileContentDifferent(fileName, content))
File.WriteAllText(fileName, content);
} public virtual String GetCustomToolNamespace(String fileName) {
return null;
} public virtual String DefaultProjectNamespace {
get { return null; }
} protected bool IsFileContentDifferent(String fileName, String newContent) {
return !(File.Exists(fileName) && File.ReadAllText(fileName) == newContent);
} private Manager(ITextTemplatingEngineHost host, StringBuilder template) {
this.host = host;
this.template = template;
} private Block CurrentBlock {
get { return currentBlock; }
set {
if (CurrentBlock != null)
EndBlock();
if (value != null)
value.Start = template.Length;
currentBlock = value;
}
} private class VSManager: Manager {
private EnvDTE.ProjectItem templateProjectItem;
private EnvDTE.DTE dte;
private Action<String> checkOutAction;
private Action<IEnumerable<String>> projectSyncAction; public override String DefaultProjectNamespace {
get {
return templateProjectItem.ContainingProject.Properties.Item("DefaultNamespace").Value.ToString();
}
} public override String GetCustomToolNamespace(string fileName) {
return dte.Solution.FindProjectItem(fileName).Properties.Item("CustomToolNamespace").Value.ToString();
} public override void Process(bool split) {
if (templateProjectItem.ProjectItems == null)
return;
base.Process(split);
projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(generatedFileNames, null, null));
} protected override void CreateFile(String fileName, String content) {
if (IsFileContentDifferent(fileName, content)) {
CheckoutFileIfRequired(fileName);
File.WriteAllText(fileName, content);
}
} internal VSManager(ITextTemplatingEngineHost host, StringBuilder template)
: base(host, template) {
var hostServiceProvider = (IServiceProvider) host;
if (hostServiceProvider == null)
throw new ArgumentNullException("Could not obtain IServiceProvider");
dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new ArgumentNullException("Could not obtain DTE from host");
templateProjectItem = dte.Solution.FindProjectItem(host.TemplateFile);
checkOutAction = (String fileName) => dte.SourceControl.CheckOutItem(fileName);
projectSyncAction = (IEnumerable<String> keepFileNames) => ProjectSync(templateProjectItem, keepFileNames);
} private static void ProjectSync(EnvDTE.ProjectItem templateProjectItem, IEnumerable<String> keepFileNames) {
var keepFileNameSet = new HashSet<String>(keepFileNames);
var projectFiles = new Dictionary<String, EnvDTE.ProjectItem>();
var originalFilePrefix = Path.GetFileNameWithoutExtension(templateProjectItem.get_FileNames()) + ".";
foreach(EnvDTE.ProjectItem projectItem in templateProjectItem.ProjectItems)
projectFiles.Add(projectItem.get_FileNames(), projectItem); // Remove unused items from the project
foreach(var pair in projectFiles)
if (!keepFileNames.Contains(pair.Key) && !(Path.GetFileNameWithoutExtension(pair.Key) + ".").StartsWith(originalFilePrefix))
pair.Value.Delete(); // Add missing files to the project
foreach(String fileName in keepFileNameSet)
if (!projectFiles.ContainsKey(fileName))
templateProjectItem.ProjectItems.AddFromFile(fileName);
} private void CheckoutFileIfRequired(String fileName) {
var sc = dte.SourceControl;
if (sc != null && sc.IsItemUnderSCC(fileName) && !sc.IsItemCheckedOut(fileName))
checkOutAction.EndInvoke(checkOutAction.BeginInvoke(fileName, null, null));
}
}
} #>

批量生成实体类(数据库访问)

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Data" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #> <#@include file="$(SolutionDir)\Manager.ttinclude"#>
<# var manager = Manager.Create(Host, GenerationEnvironment); #> <# manager.StartHeader(); #>
using System;
using System.Collections.Generic;
namespace My.Model
{
<# manager.EndBlock(); #> <#
string connectionString= "server=localhost;database=UserRight;uid=sa;pwd=123@abc;";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open(); string selectQuery ="SET FMTONLY ON; select * from @tableName; SET FMTONLY OFF;";
SqlCommand command = new SqlCommand(selectQuery,conn);
SqlDataAdapter ad = new SqlDataAdapter(command);
System.Data.DataSet ds = new DataSet();
System.Data.DataTable schema = conn.GetSchema("Tables"); foreach(System.Data.DataRow row in schema.Rows)
{
ds.Tables.Clear();
string tb_name = row["TABLE_NAME"].ToString();
command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString());
ad.FillSchema(ds,SchemaType.Mapped,tb_name);
manager.StartNewFile(tb_name+".cs");
#>
/// <summary>
/// 实体-<#=tb_name#>
/// </summary>
public partial class <#=tb_name#>
{
<#
PushIndent(" ");
foreach (DataColumn dc in ds.Tables[].Columns)
{
WriteLine("public " + dc.DataType.Name + (dc.AllowDBNull && dc.DataType.Name.ToLower() != "string" ? "? ": " ") + dc.ColumnName + " { get; set; }");
}
PopIndent();
#>
}
<#
manager.EndBlock();
}
conn.Close();
#> <# manager.StartFooter(); #>
}
<# manager.EndBlock(); #> <# manager.Process(true); #>

批量生成实体类(ADO实体对象模型)

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.xml" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Linq" #>
<#@ output extension=".cs" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace JiaHe.Entities
{
<#
string connectionString= "server=localhost;database=JiaHe;uid=sa;pwd=123@abc;";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open(); System.Data.DataTable schema = conn.GetSchema("Tables"); foreach(System.Data.DataRow row in schema.Rows)
{
string tb_name= row["TABLE_NAME"].ToString(); string selectQuery =
@"
SELECT (case when a.colorder=1 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(case when (SELECT count(*) FROM sysobjects WHERE (name in (
SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then '√' else '' end) 主键,b.name 类型,a.length 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,(case when a.isnullable=1 then '√'else '' end) 允许空,
isnull(e.text,'') 默认值,isnull(g.[value], ' ') AS [说明]
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.class and f.minor_id=0
--where b.name is not null
--WHERE d.name='TestTB' --如果只查询指定表,加上此条件
WHERE d.NAME = '" + tb_name + @"' --如果只查询指定表,加上此条件
order by a.id,a.colorder
";
SqlCommand command = new SqlCommand(selectQuery,conn);
SqlDataAdapter ad = new SqlDataAdapter(command);
System.Data.DataSet ds = new DataSet();
ad.Fill(ds); WriteLine(" public partial class " + tb_name);
WriteLine(" {");
foreach (DataRow dr in ds.Tables[].Rows)
{
string fieldType="";
switch(dr["类型"].ToString()){
case "bit":
fieldType="bool";
break; case "int":
fieldType="int";
break;
case "smallint":
fieldType="int";
break;
case "tinyint":
fieldType="int";
break;
case "bigint":
fieldType="int";
break;
case "float":
fieldType="float";
break;
case "double":
fieldType="float";
break;
case "decimal":
fieldType="float";
break; case "datetime":
fieldType="DateTime";
break;
case "date":
fieldType="DateTime";
break;
case "smalldatetime":
fieldType="DateTime";
break;
case "timestamp":
fieldType="int";
break; default:
fieldType="string";
break;
}
string temp=" public " + fieldType +" " + dr["字段名"] + " { get; set; }";
if(!string.IsNullOrEmpty(dr["说明"].ToString()))
temp += "//"+dr["说明"].ToString();
WriteLine(temp);
}
WriteLine(" }");
}
conn.Close();
#>
}

T4批量生成多文件的更多相关文章

  1. FluentData-新型轻量级ORM 利用T4模板 批量生成多文件 实体和业务逻辑 代码

    FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.Model 利用T4模板,[MultipleOutputHelper.ttinclude]批量生成多文件 ...

  2. [原创] 利用前端+php批量生成html文件,传入新文本,输出新的html文件

    本人因为要想自己写个小说网站练练手,在其中遇到的一些问题,将其解决方法总结出来,例如: 1:小说网站存储了大量的小说,每个小说主页都很相似,url不同,不是使用的history属性改写的,所以如果人工 ...

  3. T4模板批量生成代码文件

    <#@ template debug="false" hostspecific="true" language="C#" #> ...

  4. 批量生成sqlldr文件,高速卸载数据

    SQL*Loader 是用于将外部数据进行批量高速加载的数据库的最高效工具,可用于将多种平面格式文件加载到Oracle数据库.SQL*Loader支持传统路径模式以及直接路径这两种加载模式.关于SQL ...

  5. asp.net asp.net application 升级到 asp.net web 解决找不到控件 批量生成.designer文件

    颇费周折后,其实很简单,只需要生成designer文件后,重新保存所有页面即可.就是懒得写.懒真的是一种病,手上不能懒,脑子里更不能懒,否则就是给自己挖坑,仔细认真,注意细节!!!! PS:注意修改p ...

  6. 利用Resgen.exe 批量生成resources文件

    Resgen.exe(资源文件生成器)  您可以直接如图操作 转换时在 文本中先写好要转换的文件然后 全选 复制到控制台中 Filename.resx 要转换的文件 ResName1.resource ...

  7. java批量生成excel文件

    1.导入用于操作excel的jar,地址:https://pan.baidu.com/s/1qXADRlU 2.生成excel使用的模版文件,地址:https://pan.baidu.com/s/1c ...

  8. 批量生成xml文件数据C#实现

    方法一 // < Records count = "5" > //< Record > // < Contact_ID > 5 - 55W - ...

  9. Office Word文件批量生成软件

    一.软件用途 如果Word文件模板固定,只是要素信息不同,则可以使用本软件批量生成Word文件. 软件下载地址(2020-12-6更新):https://files.cnblogs.com/files ...

随机推荐

  1. HTML里的id等属性命名需要注意

    提交 $(function(){ $('#submit').click(function(){ $('#form').submit() }) }) 这里的代码无法完成提交,因为id被命名为submit ...

  2. [C] zintrin.h : 智能引入intrinsic函数。支持VC、GCC,兼容Windows、Linux、Mac OS X

    博客来源:http://blog.csdn.net/zyl910/article/details/8100744 现在很多编译器支持intrinsic函数,这给编写SSE等SIMD代码带来了方便.但是 ...

  3. 对象的引用和clone

    $a=new s(); $b=$a; 这是引用,两者为同一个实例 $a=new s(); $b=$a; $b=clone $a; 这是clone,两者不是同一个实例,但拥有相同的属性 如果需要不相同的 ...

  4. audition输出参数设置

  5. python--flask使用

    Flask是一个使用 Python 编写的轻量级 Web 应用框架.下面我将使用Flask框架,创建一个简单的html页面示例. 1.项目的目录结构如下所示:exweb\  uniqueenv\  a ...

  6. CentOS6.6 安装 Tengine 笔记

    Tengine官网上有个非常简单的教程,中间并未涉及到一些常用的设置,所以仅供参考.一下午为本人的安装步骤及过程. 1.安装必要的编译环境好 由于Tengine安装需要使用源代码自行编译,所以在安装前 ...

  7. 【性能诊断】十、性能问题综合分析(案例1,windbg、Network Monitor)

    [问题描述]:       产品中某业务功能A,在进行"刷新"->选择制单->新增->切换其他行等一系列操作后,突然发生客户端不响应的现象.       经反复测 ...

  8. final,static

    如果输入参数在方法体执行过程中,强制不能被修改,那么参数类型前加final比较安全. final修饰的函数会被编译器优化,优化意味着编译器可能将该方法用内联(inline)方式载入.final修饰变量 ...

  9. C语言常用的小代码

    圆周率Pi tan(Pi/4)=1 => Pi=4*arctan(1) 反正切函数arctan()在C语言里表示为atan(),为保证精度取圆周率的代码如下: const double Pi = ...

  10. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...