zoukankan      html  css  js  c++  java
  • 使用T4模板调用Sqlserver链接生成自己的模板

    <#@ template debug="false" hostspecific="false" language="C#" #>
    <#@ assembly name="Microsoft.CSharp" #>
    <#@ assembly name="System.Core" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ assembly name="System.Data" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#@ import namespace="System.Data.SqlClient" #>
    <#@ import namespace="System.Dynamic" #>
    <#@ output extension=".cs" #>
    <# 
        var namespaceName="Reap.DAL.Repositories";
        var connectionString = @"data source=xxx;Initial Catalog=xxx;User ID=sa;Password=xx;";   
         using (var db = new SqlConnection (connectionString))
         using (var cmd = db.CreateCommand())
         {
            db.Open();
            var tables= ReadRows (cmd, "SELECT * FROM sys.tables WHERE NAME<>'__MigrationHistory'").ToArray();
    #>
    using Reap.Models.Models;
    using Reap.IDAL.Repositories;
    using Reap.DAL.UnitOfWork;
    namespace <#=namespaceName#>{
      <#
            foreach (var table in tables)
            {         
      #>
         public class I<#=table.name#>Repository:EFRepositoryBase<<#=table.name#>>,I<#=table.name#>Repository{
            }
      <#
         }
         }
      #>
      }
      <#+
       static IEnumerable<dynamic> ReadRows (SqlCommand command, string sql)
         {
            command.CommandText = sql ?? "";
    
            using (var reader = command.ExecuteReader())
            {
               while (reader.Read())
               {
                  var dyn = new ExpandoObject ();
                  IDictionary<string, object> dic = dyn;
    
                  for (var iter = 0; iter < reader.FieldCount; ++iter)
                  {
                        dic[reader.GetName(iter) ?? ""] = reader.GetValue(iter);
                  }
                  yield return dyn;
               }
            }
         }
      #>
  • 相关阅读:
    Activity的启动模式
    Activity的生命周期
    C之静态内存和动态内存
    C之指针的加法
    C之函数返回一个以上的值
    C之交换数据案例
    C之自定义类型
    C之枚举
    联合体
    C之结构体
  • 原文地址:https://www.cnblogs.com/flyfish2012/p/3909588.html
Copyright © 2011-2022 走看看