一、一个简单的例子
这个例子仅是一个简单的应用,在我翻译并学习完CodeSmith的英文帮助文档后,对CodeSmith有了一定的了解,开始着手编写一些CodeSmith应用模板,今天按照最早提到的例子自行编写了一个基于表的添加存储过程的生成模板。具体语法前面基础中已做过详细解释这里仅是一个小综合应用的例子,望对大家学习CodeSmith有很好的帮助。我的同事也写了几个CodeSmith的技巧的文章
http://terrylee.cnblogs.com/
大家有空去看看,写的很不错哦,都是针对于CodeSmith自定义属性编写的东东:)
1
<
%@ CodeTemplate
Language
="C#"
TargetLanguage
="T-SQL"
Description
="Create a procedure which have insert function base on a table."
%
>
2 < %@ Assembly Name ="SchemaExplorer" % >
3 < %@ Import Namespace ="SchemaExplorer" % >
4 < %@ Property Name ="SourceTable" Type ="SchemaExplorer.TableSchema" Category ="DataTable" Description ="Table that the stored procedures should be based on." % >
5 < %@ Property Name ="Author" Type ="String" Category ="Context" Description ="The author for this procedure." % >
6 < %@ Property Name ="Description" Type ="String" Category ="Context" Description ="The description for this procedure." % >
7 < script runat ="template" >
8 public string GetSqlParameterStatement(ColumnSchema column)
9 {
10 string param = "@" + column.Name + " " + column.NativeType;
11 switch (column.DataType)
12 {
13 case DbType.Decimal:
14 {
15 param += "(" + column.Precision + ", " + column.Scale + ")";
16 break;
17 }
18 default:
19 {
20 if (column.Size > 0)
21 {
22 param += "(" + column.Size + ")";
23 }
24 break;
25 }
26 }
27 return param;
28 }
29 </ script >
30 CREATE PROCEDURE dbo. < %=SourceTable .Name % > Insert
31 /*
32 ==================================================
33
2 < %@ Assembly Name ="SchemaExplorer" % >
3 < %@ Import Namespace ="SchemaExplorer" % >
4 < %@ Property Name ="SourceTable" Type ="SchemaExplorer.TableSchema" Category ="DataTable" Description ="Table that the stored procedures should be based on." % >
5 < %@ Property Name ="Author" Type ="String" Category ="Context" Description ="The author for this procedure." % >
6 < %@ Property Name ="Description" Type ="String" Category ="Context" Description ="The description for this procedure." % >
7 < script runat ="template" >
8 public string GetSqlParameterStatement(ColumnSchema column)
9 {
10 string param = "@" + column.Name + " " + column.NativeType;
11 switch (column.DataType)
12 {
13 case DbType.Decimal:
14 {
15 param += "(" + column.Precision + ", " + column.Scale + ")";
16 break;
17 }
18 default:
19 {
20 if (column.Size > 0)
21 {
22 param += "(" + column.Size + ")";
23 }
24 break;
25 }
26 }
27 return param;
28 }
29 </ script >
30 CREATE PROCEDURE dbo. < %=SourceTable .Name % > Insert
31 /*
32 ==================================================
33