CodeSmith基本认识

模板以.cst为后辍名

<%@ Template Language="C#" TargetLanguage="C#" Description="" ResponseEncoding="UTF-8"%>

Language:该模板使用的语言; TargetLanguage:目标语言,将会生成的语言。

Description:描述;ResponseEncoding:编码

<%@ Property Name="Author" Type="String" Category="Context" Default=""
Description="" Optional="true"%>

Property :参数(要定义的参数);Name:参数名称;Type:参数类型

Category:属性分类的名称;Default:默认值;Optional:是否可选;

数据库访问组件SchemaExplorer

引用:<%@ Assembly Name="SchemaExplorer"%>

 导入:<%@ Import Namespace="SchemaExplorer"%>

组件的使用:(常为引用的数据库中数据类型)
SchemaExplorer.DatabaseSchema:数据库
SchemaExplorer.TableSchema:表
SchemaExploror.ColumnSchema:列
SchemaExploror.ViewSchema:视图

表常用的属性:
ForeignKeyColumns:所有外键列的集合
NonForeignKeyColumns:所有非外键列的集合
NonKeyColumns:所有不是主键又不是外键的列的集合

KeyColumns主键列集合

如:<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Cate
gory="Context" Description="" %>

若想访问视图,变量类型为chemaExplorer.TableSchema;想访问数据库,则变量类型为SchemaExplorer.DatabaseSchema

 

创建存储过程

CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < Source
Table.Columns.Count - 1) { %>,<% } %>
<% } %>

as

update <%=SourceTable.Name %> set <% for(int i=0;i<SourceTable.NonPrimaryKeyColumns.Count;i++){ %>

<% =SourceTable.NonPrimaryKeyColumns.Count[i].Name %>=@

<%=SourceTable.NonPrimaryKeyColumns.Count[i].Name %>

<% if (i<SourceTable.NonPrimaryKeyColumns.Count-1){ %>,<% }%>

<% }%> 

where <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.C
ount; i++) { %>

<%if (i>0) { %>

and <% }%>

 <%=SourceTable.PrimaryKey.MemberColumns[i].Name %>=@

<%=SourceTable.PrimaryKey.MemberColumns[i]Name %>

<% } %>

 脚本标签:
在这个标签中可以包含一段代码,它们不直接影响输出的模板,在里面存放一些常用的方法

 <script runat="template">
public string GetSqlParameterStatement(ColumnSchema column)
 {
 string param = "@" + column.Name + "  " + column.NativeType;

 switch (column.DataType)
 {
  case DbType.Decimal:
  {
  param += "(" + column.Precision + ", " + column.Scale + ")";
  break;
 }
 default:
 {
  if (column.Size > 0)
  {
 param += "(" + column.Size + ")";
  }

  break;
  }
  }
 
  return param;

 </script>

 

创建一个可以下拉选择的属性 :首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型

  <%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category
="Collection" Description="Type of collection" %>
 
  <script runat="tempate">
  public enum CollectionTypeEnum
  {
  Vector,
  HashTable,
  SortedList
 }
  </script>

 声明一个常量

<script runat="template">
private const string MY_CONST = "example";
</script>

对模板进行调试
如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用
Debugger.Break()语句设置断点即可。
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Deb
ugging your template" Debug="true" %>
<% Debugger.Break(); %>

将属性设置成选择一个文件夹的路径

[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System
.Drawing.Design.UITypeEditor))]
public string OutputDirectory
{
get {return _outputDirectory;}
set {_outputDirectory= value;}
}

调用子模板

<% foreach (TableSchema table in SourceDatabase.Tables) { OutputSubTemplate(table); } %>

<script runat="template">

private CodeTemplate _mySubTemplate;

[Browsable(false)]

public CodeTemplate MySubTemplate

{

  get{ if(_mySubTemplate==null){

    CodeTemplateCompiler compiler=new CodeTemplateCompiler(this.Co
          deTemplateInfo.DirectoryName + "MySubTemplate.cst");

            compiler.Compile();

    if(compiler.Errors.Count==0)

    {

      _mySubTemplate = compiler.CreateInstance();

    }

    else

    {

      for (int i = 0; i < compiler.Errors.Count; i++)
          {
        Response.WriteLine(compiler.Errors[ i].ToString());
        }

     }

    }

    return _mySubTemplate;

  }

}

public void OutputSubTemplate(TableSchema table)

{

  MySubTemplate.SetProperty("SourceTable", table);
   MySubTemplate.SetProperty("IncludeDrop", false);
    MySubTemplate.SetProperty("InsertPrefix", "Insert");
    MySubTemplate.Render(Response);

}

</script>

   使用SchemaExplorer能否确定一个字段(Field)是标识字段(主键,Identity Field)
在字段的扩展属性集合中包含一个叫“CS_IsIdentity”的属性,如果这个属性的值为
true,则表名当前字段为一个标识字段

Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {
  if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) == true)
 {
  Response.Write(cs.Name);
 }
  }
  %>

    如何确定一个字段的默认值(各人认为翻译成如何知道一个字段有默认值并且默认
值是什么)
在字段的扩展属性集合中包含一个叫“CS_Default”的属性

 <%foreach(ColumnSchema cs in SourceTable.Columns) {
  if (cs.ExtendedProperties["CS_Default"] != null)
  {
  Response.WriteLine(cs.ExtendedProperties["CS_Default"].Value);
  }
  }
  %>

使用SchemaExplorer得到存储过程的输入输出参数

Input Parameters:
  <%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
  {
  Response.Write(ps.Name);
  Response.Write("\n");
  }
  %>
  Output Parameters:
  <%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
  {
  Response.Write(ps.Name);
 Response.Write("\n");
 }
  %>

 

 ColumnSchema的一些常用属性,如下表: 

属性Property

描述Deion

AllowDBNull

是否允许空值NULL

Database

通过DatabaseSchema对象得到当前列所属的数据库

DataType

此数据对象的数据类型

Deion

当前对象的描述

ExtendedProperties

用来存储SchemaObject的其他附加信息

IsForeignKeyMember

当前列是否为外键

IsPrimaryKeyMember

当前列是否为主键

IsUnique

当前列是否唯一

Name

列的名称

NativeType

列定义的数据类型

Precision

数据对象的精度

Scale

数据对象的范围(个人理解为需要保留小数的范围)

Size

数据对象的大小(例如:字符串长度为10)

SystemType

数据对象的系统类型

Table

当前列所属的数据表

posted @ 2012-11-26 23:43  skylionsea  阅读(221)  评论(0)    收藏  举报