程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> CodeSmith生成SQL Server視圖的實體類腳本

CodeSmith生成SQL Server視圖的實體類腳本

編輯:關於SqlServer

  第一個文件是businessobjctforVIEw.cst,生成C#實體類,此腳本需要引用第二個文件CommonUtility.cs。需將兩個文件放在同一目錄中。

<   Description="Generatesaupdatestoredprocedure."%>
<
%@AssemblyName="SchemaExplorer"%>
<%@ImportNamespace="SchemaExplorer"%>
<   Category="Context"
   Description="Tablethatthestoredproceduresshouldbebasedon."%>
<
%@AssemblySrc="CommonUtility.cs"%>
<%@ImportNamespace="Common.Data"%>
<scriptrunat="template">
  CommonUtilityrule=newCommonUtility();
</script>
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Data;                                                                            
namespaceSOA.Model
{
  ///<summary>
  ///<%=SourceTable.Name%>objectforVIEwtable'<%=SourceTable.Name%>'.
  ///</summary>
  [Serializable]
  publicclass<%=SourceTable.Name%>
  {
  #regionPrivateMember
    <%for(inti=0;i<SourceTable.Columns.Count;i++){%>
    private<%=rule.GetCSharpVariableType(SourceTable.Columns[i])%>_<%=SourceTable.Columns[i].Name%>;
  <%}%>
  #endregion
    
  #regionConstructor
    public<%=SourceTable.Name%>(){}    
    
    public<%=SourceTable.Name%>(
        <%for(inti=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(inti=0;i<SourceTable.Columns.Count;i++){%>
    _<%=SourceTable.Columns[i].Name%>=<%=SourceTable.Columns[i].Name%>;
  <%}%>
    }
  #endregion
  
  #regionPublicPropertIEs
  <%for(inti=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#的數據類型定義以及輸出:

usingSystem;
usingSystem.Text;
usingCodeSmith.Engine;
usingSchemaExplorer;
usingSystem.ComponentModel;
usingSystem.Data;
namespaceCommon.Data
{
  /**////<summary>
  ///TemplateRule
  ///</summary>
  publicclassCommonUtility
  {
    //getColumnsinfobyTableName
    publicViewColumnScheMacollectionGetColumnCollectionByTable(VIEwSchematable)
    {
      ViewColumnScheMacollectioncolumns=newVIEwColumnScheMacollection(table.Columns);
      returncolumns;
    }
    //Getcamelcasename,suchasCustomer,
    publicstringGetCamelCaseName(stringstr)
    {
      returnstr.Substring(0,1).ToUpper()+str.Substring(1);
    }
    
   //Get,user,privateconstStringUSER_FIELD="User"
    publicstringGetMemberConstantDeclarationStatement(ColumnScheMacolumn)
    {
      returnGetMemberConstantDeclarationStatement("publicconstString",column);
    }
    
    //suchASPublicconstStringUSER_TABLE="User"
    publicstringGetTableConstantDeclarationStatement(VIEwSchematable)
    {
      returnGetMemberConstantDeclarationStatement("publicconstString",table);  
    }
    //suckasUSER_TABLE
    publicstringGetUpperStatement(VIEwSchematable)
    {
      return  table.Name.ToUpper()+"_TABLE";
   }
    //suckasUSER_FIELD
    publicstringGetUpperStatement(ColumnScheMacolumn)
   {
     returncolumn.Name.ToUpper()+"_FIELD";
    }
    //suchasUSER_TABLE="User"
    publicstringGetMemberConstantDeclarationStatement(stringprotectionLevel,VIEwSchematable)
    {
      returnprotectionLevel+GetUpperStatement(table)+"="+GetCamelCaseName(table.Name)+"";
    }
   
    //suchasUSERID_FIELD="Userid"
    publicstringGetMemberConstantDeclarationStatement(stringprotectionLevel,ColumnScheMacolumn)
    {
      returnprotectionLevel+GetUpperStatement(column)+"="+GetCamelCaseName(column.Name)+"";
    }
    publicstringGetCSharpVariableType(VIEwColumnScheMacolumn)
    {
      switch(column.DataType)
      {
        caseDbType.AnsiString:return"string";
        caseDbType.AnsiStringFixedLength:return"string";
        caseDbType.Binary:return"byte[]";
        caseDbType.Boolean:return"bool";
        caseDbType.Byte:return"int";
        caseDbType.Currency:return"decimal";



        caseDbType.Date:return"DataTime";
        caseDbType.DateTime:return"DateTime";
        caseDbType.Decimal:return"decimal";
        caseDbType.Double:return"double";
        caseDbType.Guid:return"Guid";
        caseDbType.Int16:return"short";
        caseDbType.Int32:return"int";
        caseDbType.Int64:return"long";
        caseDbType.Object:return"object";
        caseDbType.SByte:return"sbyte";
        caseDbType.Single:return"float";
        caseDbType.String:return"string";
        caseDbType.StringFixedLength:return"string";
        caseDbType.Time:return"TimeSpan";
        caseDbType.UInt16:return"ushort";
        caseDbType.UInt32:return"uint";
        caseDbType.UInt64:return"ulong";
        caseDbType.VarNumeric:return"decimal";
      }
      
      returnnull;
    }
    
    publicstringGetCSharpBaseType(VIEwColumnScheMacolumn)
   {
      switch(column.DataType)
     {
        caseDbType.AnsiString:return"System.String";
        caseDbType.AnsiStringFixedLength:return"System.String";
        caseDbType.Binary:return"System.Byte[]";
        caseDbType.Boolean:return"System.Boolean";
        caseDbType.Byte:return"System.Int32";
        caseDbType.Currency:return"System.Decimal";
        caseDbType.Date:return"System.DataTime";
        caseDbType.DateTime:return"System.DataTime";
        caseDbType.Decimal:return"System.Decimal";
        caseDbType.Double:return"System.Double";
        caseDbType.Guid:return"System.Guid";
        caseDbType.Int16:return"System.Int16";
        caseDbType.Int32:return"System.Int32";
        caseDbType.Int64:return"System.Int64";
        caseDbType.Object:return"System.Object";
        caseDbType.SByte:return"System.SByte";
        caseDbType.Single:return"System.Single";
        caseDbType.String:return"System.String";
        caseDbType.StringFixedLength:return"System.String";
        caseDbType.Time:return"System.TimeSpan";
        caseDbType.UInt16:return"System.UInt16";
        caseDbType.UInt32:return"System.UInt32";
        caseDbType.UInt64:return"System.UInt64";
        caseDbType.VarNumeric:return"System.Decimal";
      }
      returnnull;
    }
  }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved