nawawa001 发表于 2015-7-1 14:32:17

CodeSmith生成SQL Server视图的实体类脚本

  第一个文件是businessobjctforView.cst,生成C#实体类,此脚本需要引用第二个文件CommonUtility.cs。需将两个文件放在同一目录中。








    CommonUtility rule=new CommonUtility();

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;                                                                                                                                                         

namespace SOA.Model
{
    ///
    ///object for View table ''.
    ///
   
    public class
    {
    #region Private Member
      
      private_;
   
    #endregion
      
    #region Constructor
      public () {}      
      
      public (
               
         ,   
   
   
      )
      {
      
         _ = ;
   
      }
    #endregion
   
    #region Public Properties
   
      public
      {
            get { return _; }
            set { _ = value; }
      }
   
    #endregion
    }
}

第二个文件是CommonUtility.cs,此文件是包含C#的数据类型定义以及输出:

Code
using System;
using System.Text;
using CodeSmith.Engine;
using SchemaExplorer;
using System.ComponentModel;
using System.Data;
namespace Common.Data
{
    /**////
    /// TemplateRule
    ///
    public class CommonUtility
    {
      //get Columns info by TableName
      public ViewColumnSchemaCollection GetColumnCollectionByTable(ViewSchema table)
      {
            ViewColumnSchemaCollection columns = new ViewColumnSchemaCollection(table.Columns);
            return columns;
      }
      //Get camelcase name,such as Customer,
      public string GetCamelCaseName(string str)
      {
            return str.Substring(0,1).ToUpper()+str.Substring(1);
      }
      
       //Get ,user,private const String USER_FIELD = "User"
      public string GetMemberConstantDeclarationStatement(ColumnSchema column)
      {
            return GetMemberConstantDeclarationStatement("public const String ",column);
      }
      
      //such as public const String USER_TABLE = "User"
      public string GetTableConstantDeclarationStatement(ViewSchema table)
      {
            return GetMemberConstantDeclarationStatement("public const String ",table);   
      }
      //suck as USER_TABLE
      public string GetUpperStatement(ViewSchema table)
      {
            return   table.Name.ToUpper()+"_TABLE";
       }
      //suck as USER_FIELD
      public string GetUpperStatement(ColumnSchema column)
       {
         return column.Name.ToUpper()+"_FIELD";
      }
      // such as USER_TABLE = "User"
      public string GetMemberConstantDeclarationStatement(string protectionLevel, ViewSchema table)
      {
            return protectionLevel+GetUpperStatement(table)+" = "+GetCamelCaseName(table.Name)+"";
      }
      
      //such as USERID_FIELD = "Userid"
      public string GetMemberConstantDeclarationStatement(string protectionLevel,ColumnSchema column)
      {
            return protectionLevel+GetUpperStatement(column)+" = "+GetCamelCaseName(column.Name)+"";
      }
      public string GetCSharpVariableType(ViewColumnSchema column)
      {
            switch(column.DataType)
            {
                case DbType.AnsiString: return "string";
                case DbType.AnsiStringFixedLength: return "string";
                case DbType.Binary: return "byte[]";
                case DbType.Boolean: return "bool";
                case DbType.Byte: return "int";
                case DbType.Currency: return "decimal";
                case DbType.Date: return "DataTime";
                case DbType.DateTime: return "DateTime";
                case DbType.Decimal: return "decimal";
                case DbType.Double: return "double";
                case DbType.Guid: return "Guid";
                case DbType.Int16: return "short";
                case DbType.Int32: return "int";
                case DbType.Int64: return "long";
                case DbType.Object: return "object";
                case DbType.SByte: return "sbyte";
                case DbType.Single: return "float";
                case DbType.String: return "string";
                case DbType.StringFixedLength: return "string";
                case DbType.Time: return "TimeSpan";
                case DbType.UInt16: return "ushort";
                case DbType.UInt32: return "uint";
                case DbType.UInt64: return "ulong";
                case DbType.VarNumeric: return "decimal";
            }
            
            return null;
      }
      
      public string GetCSharpBaseType(ViewColumnSchema column)
       {
            switch(column.DataType)
         {
                case DbType.AnsiString: return "System.String";
                case DbType.AnsiStringFixedLength: return "System.String";
                case DbType.Binary: return "System.Byte[]";
                case DbType.Boolean: return "System.Boolean";
                case DbType.Byte: return "System.Int32";
                case DbType.Currency: return "System.Decimal";
                case DbType.Date: return "System.DataTime";
                case DbType.DateTime: return "System.DataTime";
                case DbType.Decimal: return "System.Decimal";
                case DbType.Double: return "System.Double";
                case DbType.Guid: return "System.Guid";
                case DbType.Int16: return "System.Int16";
                case DbType.Int32: return "System.Int32";
                case DbType.Int64: return "System.Int64";
                case DbType.Object: return "System.Object";
                case DbType.SByte: return "System.SByte";
                case DbType.Single: return "System.Single";
                case DbType.String: return "System.String";
                case DbType.StringFixedLength: return "System.String";
                case DbType.Time: return "System.TimeSpan";
                case DbType.UInt16: return "System.UInt16";
                case DbType.UInt32: return "System.UInt32";
                case DbType.UInt64: return "System.UInt64";
                case DbType.VarNumeric: return "System.Decimal";
            }
            return null;
      }
    }
}
页: [1]
查看完整版本: CodeSmith生成SQL Server视图的实体类脚本