设为首页 收藏本站
查看: 605|回复: 0

[经验分享] CodeSmith生成SQL Server视图的实体类脚本

[复制链接]

尚未签到

发表于 2015-7-1 14:32:17 | 显示全部楼层 |阅读模式
  第一个文件是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 ''.
    ///
    [Serializable]
    public class
    {
    #region Private Member
        
        private  _;
   
    #endregion
        
    #region Constructor
        public () {}        
        
        public (
               
         ,   
   
     
        )
        {
        
         _ = ;
   
        }
    #endregion
   
    #region Public Properties
   
        public  
        {
            get { return _; }
            set { _ = value; }
        }
   
    #endregion
    }
}

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

DSC0000.gif DSC0001.gif 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-82285-1-1.html 上篇帖子: MS SQL SERVER SUSPECT数据库置疑/可疑状态后处理办法 下篇帖子: 翻译:改进.NET应用程序的性能和可伸缩性(四)-SQL Server性能
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表