当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

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

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

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
      Description="Generates a update stored procedure." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.ViewSchema"
      Category="Context"
      Description="Table that the stored procedures should be based on." %>
<%@ Assembly Src="CommonUtility.cs" %>
<%@ Import Namespace="Common.Data" %>
<script runat="template">
    CommonUtility rule=new CommonUtility();
</script>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;                                                                                                                                                        

namespace SOA.Model
{
    /// <summary>
    /// <%= SourceTable.Name %> object for View table '<%= SourceTable.Name %>'.
    /// </summary>
    [Serializable]
    public class <%= SourceTable.Name %>
    {
    #region Private Member
        <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
        private <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> _<%= SourceTable.Columns[i].Name %>;
    <% } %>
    #endregion
       
    #region Constructor
        public <%= SourceTable.Name %>() {}       
       
        public <%= SourceTable.Name %>(
                <% for (int i = 0; i < SourceTable.Columns.Count-1; i++) { %>
        <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> <%= SourceTable.Columns[i].Name %>,   
    <% } %>
    <%= rule.GetCSharpVariableType(SourceTable.Columns[SourceTable.Columns.Count-1]) %> <%= SourceTable.Columns[SourceTable.Columns.Count-1].Name %>
        )
        {
        <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
         _<%= SourceTable.Columns[i].Name %> = <%= SourceTable.Columns[i].Name %>;
    <% } %>
        }
    #endregion
   
    #region Public Properties
    <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
        public <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> <%= rule.GetCamelCaseName(SourceTable.Columns[i].Name) %>
        {
            get { return _<%= SourceTable.Columns[i].Name %>; }
            set { _<%= SourceTable.Columns[i].Name %> = 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
{
    /**//// <summary>
    /// TemplateRule
    /// </summary>
    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

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接