CodeSmith应用(一)

CodeSmith添加存储过程生成模板示例
作者在学习CodeSmith英文帮助文档后,编写基于表的添加存储过程的生成模板。文中给出了具体代码示例,包含属性定义、方法编写及存储过程创建等内容,希望对大家学习CodeSmith有帮助。
        这个例子仅是一个简单的应用,在我翻译并学习完CodeSmith的英文帮助文档后,对CodeSmith有了一定的了解,开始着手编写一些CodeSmith应用模板,今天按照最早提到的例子自行编写了一个基于表的添加存储过程的生成模板。具体语法前面基础中已做过详细解释这里仅是一个小综合应用的例子,望对大家学习CodeSmith有很好的帮助。我的同事也写了几个CodeSmith的技巧的文章http://terrylee.cnblogs.com/大家有空去看看,写的很不错哦,都是针对于CodeSmith自定义属性编写的东东:)
 1 None.gif < %@ CodeTemplate  Language ="C#"  TargetLanguage ="T-SQL"  Description ="Create a procedure which have insert function base on a table."  % >
 2 None.gif < %@ Assembly  Name ="SchemaExplorer"  % >
 3 None.gif < %@ Import  Namespace ="SchemaExplorer"  % >
 4 None.gif < %@ Property  Name ="SourceTable"  Type ="SchemaExplorer.TableSchema"  Category ="DataTable"  Description ="Table that the stored procedures should be based on."  % >
 5 None.gif < %@ Property  Name ="Author"  Type ="String"  Category ="Context"  Description ="The author for this procedure." % >
 6 None.gif < %@ Property  Name ="Description"  Type ="String"  Category ="Context"  Description ="The description for this procedure." % >
 7 None.gif < script  runat ="template" >
 8 None.gifpublic string GetSqlParameterStatement(ColumnSchema column)
 9 None.gif{
10 None.gif    string param = "@" + column.Name + " " + column.NativeType;
11 None.gif    switch (column.DataType)
12 None.gif    {
13 None.gif        case DbType.Decimal:
14 None.gif        {
15 None.gif            param += "(" + column.Precision + ", " + column.Scale + ")";
16 None.gif            break;
17 None.gif        }
18 None.gif        default:
19 None.gif        {
20 None.gif            if (column.Size > 0)
21 None.gif            {
22 None.gif                param += "(" + column.Size + ")";
23 None.gif            }
24 None.gif            break;
25 None.gif        }
26 None.gif    }
27 None.gif    return param;
28 None.gif}
29 None.gif </ script >
30 None.gifCREATE PROCEDURE dbo. < %=SourceTable .Name % > Insert
31 None.gif/*
32 None.gif==================================================
33 None.gifAuthor: < %= Author  % >
34 None.gifCreatedTime: < %= System .DateTime.Now.ToShortDateString() % >
35 None.gifDescription: < %= Description  % >
36 None.gif==================================================
37 None.gif*/
38 None.gif < % for  (int i  = 0;  i < SourceTable.Columns.Count; i++) { % >
39 None.gif < %= GetSqlParameterStatement (SourceTable.Columns[i]) % >< % if  (i < SourceTable.Columns.Count - 1) { % > , < % } % >      < % if  (SourceTable.Columns[i].Description ! = "" ) { % > -- < %= SourceTable .Columns[i].Description % >< % } % >
40 None.gif < % } % >
41 None.gifAS
42 None.gifInsert Into [ < %= SourceTable .Name % >
43 None.gif(
44 None.gif < % for  (int i  = 0;  i < SourceTable.Columns.Count; i++) { % >
45 None.gif[ < %= SourceTable .Columns[i].Name % > ] < % if  (i < SourceTable.Columns.Count - 1) { % > , < % } % >      < % if  (SourceTable.Columns[i].Description ! = "" ) { % > -- < %= SourceTable .Columns[i].Description % >< % } % >
46 None.gif < % } % >
47 None.gif)
48 None.gifValues
49 None.gif(
50 None.gif < % for  (int i  = 0;  i < SourceTable.Columns.Count; i++) { % >
51 None.gif@ < %= SourceTable .Columns[i].Name % >< % if  (i < SourceTable.Columns.Count - 1) { % > , < % } % >
52 None.gif < % } % >
53 None.gif)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值