这个例子仅是一个简单的应用,在我翻译并学习完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">
8public 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>
30CREATE PROCEDURE dbo.<%=SourceTable.Name %>Insert
31/*
32==================================================
33Author:<%= Author %>
34CreatedTime:<%= System.DateTime.Now.ToShortDateString() %>
35Description:<%= Description %>
36==================================================
37*/
38<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
39<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %> <% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
40<% } %>
41AS
42Insert Into [<%= SourceTable.Name %>]
43(
44<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
45[<%= SourceTable.Columns[i].Name %>]<% if (i < SourceTable.Columns.Count - 1) { %>,<% } %> <% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
46<% } %>
47)
48Values
49(
50<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
51@<%= SourceTable.Columns[i].Name %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
52<% } %>
53)
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">
8public 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>
30CREATE PROCEDURE dbo.<%=SourceTable.Name %>Insert
31/*
32==================================================
33Author:<%= Author %>
34CreatedTime:<%= System.DateTime.Now.ToShortDateString() %>
35Description:<%= Description %>
36==================================================
37*/
38<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
39<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %> <% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
40<% } %>
41AS
42Insert Into [<%= SourceTable.Name %>]
43(
44<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
45[<%= SourceTable.Columns[i].Name %>]<% if (i < SourceTable.Columns.Count - 1) { %>,<% } %> <% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
46<% } %>
47)
48Values
49(
50<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
51@<%= SourceTable.Columns[i].Name %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
52<% } %>
53)