• 使用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;
               }
            }
         }
      #>
  • 相关阅读:
    LR实战之Discuz开源论坛——安装及简介
    LR如何利用siteScope监控MySQL性能
    初学SSH(其一)
    使用JUnit单元测试入门
    理解java中【同步】和【死锁】
    LR性能测试应用
    (28)ElasticSearch分布式架构特点
    (27)ElasticSearch 复合查询
    (06)Gitlab设置开启自启动、关闭开机自启动
    (05)安装GitLab
  • 原文地址:https://www.cnblogs.com/flyfish2012/p/3909588.html
Copyright © 2020-2023  润新知