1.如何在模板中添加註釋
CodeSmith:
<%-- Comments --%>
VB.NET:
<%-- 'Comments --%>
C#:
<%-- // Comments --%>
<%-- /* Comments */ --%>
2.創建一個可以下拉選擇的屬性
首先定義一個枚舉類型的變量,然後將屬性的類型設置爲枚舉型
1<%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category="Collection" Description="Type of collection"%>
2
3<script runat="tempate">
4public enum CollectionTypeEnum
5{
6 Vector,
7 HashTable,
8 SortedList
9}
10</script>
2
3<script runat="tempate">
4public enum CollectionTypeEnum
5{
6 Vector,
7 HashTable,
8 SortedList
9}
10</script>
3.解決ASP.NET中標籤<%重複問題
先將ASP.NET中使用的這個重複標籤寫成<%%,避免在生成代碼時由於是標籤重複引起的編譯錯誤或生成錯誤。
4.如何聲明一個常量
<script runat="template">
private const string MY_CONST ="example";
</script>
private const string MY_CONST ="example";
</script>
5.如何對模板進行調試
如果要調試一個模板,首先要在代碼模板裏進行聲明,然後在你想要進行調試的地方用Debugger.Break()語句設置斷點即可。
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Debugging your template" Debug="true"%>
<% Debugger.Break(); %>
<% Debugger.Break(); %>
6.如何將屬性設置成選擇一個文件夾的路徑
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
publicstring OutputDirectory
{
get {return _outputDirectory;}
set {_outputDirectory= value;}
}
publicstring OutputDirectory
{
get {return _outputDirectory;}
set {_outputDirectory= value;}
}
7.怎樣調用子模板
1<%
2foreach (TableSchema table in SourceDatabase.Tables)
3{
4 OutputSubTemplate(table);
5}
6%>
7<script runat="template">
8private CodeTemplate _mySubTemplate;
9
10[Browsable(false)]
11public CodeTemplate MySubTemplate
12{
13get
14{
15if (_mySubTemplate ==null)
16{
17 CodeTemplateCompiler compiler =new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName +"MySubTemplate.cst");
18 compiler.Compile();
19if (compiler.Errors.Count ==0)
20{
21 _mySubTemplate = compiler.CreateInstance();
22 }
23else
24{
25for (int i =0; i < compiler.Errors.Count; i++)
26{
27 Response.WriteLine(compiler.Errors[ i].ToString());
28 }
29 }
30 }
31return _mySubTemplate;
32 }
33}
34
35publicvoid OutputSubTemplate(TableSchema table)
36{
37 MySubTemplate.SetProperty("SourceTable", table);
38 MySubTemplate.SetProperty("IncludeDrop", false);
39 MySubTemplate.SetProperty("InsertPrefix", "Insert");
40 MySubTemplate.Render(Response);
41}
42</script>
FAQ中給出的例子爲生成一個數據庫中所有表的更新Update存儲過程2foreach (TableSchema table in SourceDatabase.Tables)
3{
4 OutputSubTemplate(table);
5}
6%>
7<script runat="template">
8private CodeTemplate _mySubTemplate;
9
10[Browsable(false)]
11public CodeTemplate MySubTemplate
12{
13get
14{
15if (_mySubTemplate ==null)
16{
17 CodeTemplateCompiler compiler =new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName +"MySubTemplate.cst");
18 compiler.Compile();
19if (compiler.Errors.Count ==0)
20{
21 _mySubTemplate = compiler.CreateInstance();
22 }
23else
24{
25for (int i =0; i < compiler.Errors.Count; i++)
26{
27 Response.WriteLine(compiler.Errors[ i].ToString());
28 }
29 }
30 }
31return _mySubTemplate;
32 }
33}
34
35publicvoid OutputSubTemplate(TableSchema table)
36{
37 MySubTemplate.SetProperty("SourceTable", table);
38 MySubTemplate.SetProperty("IncludeDrop", false);
39 MySubTemplate.SetProperty("InsertPrefix", "Insert");
40 MySubTemplate.Render(Response);
41}
42</script>
SubTemplatesExample.cst文件源代碼
1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Generates a update stored procedure."%>
3
4<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
5 Category="Context"
6 Description="Database"%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12<%
13foreach (TableSchema table in SourceDatabase.Tables)
14{
15 OutputSubTemplate(table);
16}
17%>
18
19<script runat="template">
20private CodeTemplate _mySubTemplate;
21
22
23
24
25[Browsable(false)]
26public CodeTemplate MySubTemplate
27{
28get
29{
30if (_mySubTemplate ==null)
31{
32 CodeTemplateCompiler compiler =new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName +"MySubTemplate.cst");
33 compiler.Compile();
34
35if (compiler.Errors.Count ==0)
36{
37 _mySubTemplate = compiler.CreateInstance();
38 }
39else
40{
41for (int i =0; i < compiler.Errors.Count; i++)
42{
43 Response.WriteLine(compiler.Errors[ i].ToString());
44 }
45 }
46 }
47
48return _mySubTemplate;
49 }
50}
51
52publicvoid OutputSubTemplate(TableSchema table)
53{
54 MySubTemplate.SetProperty("SourceTable", table);
55 MySubTemplate.SetProperty("IncludeDrop", false);
56 MySubTemplate.SetProperty("InsertPrefix", "Insert");
57
58 MySubTemplate.Render(Response);
59}
60</script>
MySubTemplate.cst文件源代碼2 Description="Generates a update stored procedure."%>
3
4<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
5 Category="Context"
6 Description="Database"%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12<%
13foreach (TableSchema table in SourceDatabase.Tables)
14{
15 OutputSubTemplate(table);
16}
17%>
18
19<script runat="template">
20private CodeTemplate _mySubTemplate;
21
22
23
24
25[Browsable(false)]
26public CodeTemplate MySubTemplate
27{
28get
29{
30if (_mySubTemplate ==null)
31{
32 CodeTemplateCompiler compiler =new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName +"MySubTemplate.cst");
33 compiler.Compile();
34
35if (compiler.Errors.Count ==0)
36{
37 _mySubTemplate = compiler.CreateInstance();
38 }
39else
40{
41for (int i =0; i < compiler.Errors.Count; i++)
42{
43 Response.WriteLine(compiler.Errors[ i].ToString());
44 }
45 }
46 }
47
48return _mySubTemplate;
49 }
50}
51
52publicvoid OutputSubTemplate(TableSchema table)
53{
54 MySubTemplate.SetProperty("SourceTable", table);
55 MySubTemplate.SetProperty("IncludeDrop", false);
56 MySubTemplate.SetProperty("InsertPrefix", "Insert");
57
58 MySubTemplate.Render(Response);
59}
60</script>
1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Generates a update stored procedure."%>
3
4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table that the stored procedures should be based on."%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12
13<script runat="template">
14publicstring GetSqlParameterStatement(ColumnSchema column)
15{
16string param ="@"+ column.Name +""+ column.NativeType;
17
18switch (column.DataType)
19{
20case DbType.Decimal:
21{
22 param +="("+ column.Precision +", "+ column.Scale +")";
23break;
24 }
25default:
26{
27if (column.Size >0)
28{
29 param +="("+ column.Size +")";
30 }
31break;
32 }
33 }
34
35return param;
36}
37</script>
38
39-----------------------------------------------------------------
40-- Date Created: <%= DateTime.Now.ToLongDateString() %>
41-- Created By: Generated by CodeSmith
42-----------------------------------------------------------------
43
44CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
45<%for (int i =0; i < SourceTable.Columns.Count; i++) { %>
46<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><%if (i < SourceTable.Columns.Count -1) { %>,<% }%>
47<% }%>
48AS
49
50UPDATE [<%= SourceTable.Name %>] SET
51<%for (int i =0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
52 [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><%if (i < SourceTable.NonPrimaryKeyColumns.Count -1) { %>,<% }%>
53<% }%>
54WHERE
55<%for (int i =0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
56<%if (i >0) { %>AND <% }%>
57 [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
58<% }%>
2 Description="Generates a update stored procedure."%>
3
4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table that the stored procedures should be based on."%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12
13<script runat="template">
14publicstring GetSqlParameterStatement(ColumnSchema column)
15{
16string param ="@"+ column.Name +""+ column.NativeType;
17
18switch (column.DataType)
19{
20case DbType.Decimal:
21{
22 param +="("+ column.Precision +", "+ column.Scale +")";
23break;
24 }
25default:
26{
27if (column.Size >0)
28{
29 param +="("+ column.Size +")";
30 }
31break;
32 }
33 }
34
35return param;
36}
37</script>
38
39-----------------------------------------------------------------
40-- Date Created: <%= DateTime.Now.ToLongDateString() %>
41-- Created By: Generated by CodeSmith
42-----------------------------------------------------------------
43
44CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
45<%for (int i =0; i < SourceTable.Columns.Count; i++) { %>
46<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><%if (i < SourceTable.Columns.Count -1) { %>,<% }%>
47<% }%>
48AS
49
50UPDATE [<%= SourceTable.Name %>] SET
51<%for (int i =0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
52 [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><%if (i < SourceTable.NonPrimaryKeyColumns.Count -1) { %>,<% }%>
53<% }%>
54WHERE
55<%for (int i =0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
56<%if (i >0) { %>AND <% }%>
57 [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
58<% }%>
8.在加載模板時默認加載的命名空間Namespaces和組件Assemblies
組件:mscorlib, System, System.Xml, System.Data, System.Drawing, Microsoft.VisualBasic, System.Windows.Forms, CodeSmith.Engine
命名空間:System, System.Data, System.Diagnostics, System.ComponentModel, Microsoft.VisualBasic, CodeSmith.Engine
9.使用SchemaExplorer能否確定一個字段(Field)是標識字段(主鍵,Identity Field)
在字段的擴展屬性集合中包含一個叫“CS_IsIdentity”的屬性,如果這個屬性的值爲true,則表名當前字段爲一個標識字段
1Identity Field =<%foreach(ColumnSchema cs in SourceTable.Columns) {
2if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) ==true)
3 {
4 Response.Write(cs.Name);
5 }
6}
7%>
CS_Identity_Example.cst文件源代碼2if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) ==true)
3 {
4 Response.Write(cs.Name);
5 }
6}
7%>
1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Identifies the identity field of a table"%>
3
4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table to target."%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12
13
14Identity Field =<%foreach(ColumnSchema cs in SourceTable.Columns) {
15if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) ==true)
16 {
17 Response.Write(cs.Name);
18 }
19 }
20%>
2 Description="Identifies the identity field of a table"%>
3
4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table to target."%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12
13
14Identity Field =<%foreach(ColumnSchema cs in SourceTable.Columns) {
15if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) ==true)
16 {
17 Response.Write(cs.Name);
18 }
19 }
20%>
10.如何確定一個字段的默認值(各人認爲翻譯成如何知道一個字段有默認值並且默認值是什麼)
在字段的擴展屬性集合中包含一個叫“CS_Default”的屬性
1<%
2foreach(ColumnSchema cs in SourceTable.Columns) {
3if (cs.ExtendedProperties["CS_Default"] !=null)
4 {
5 Response.WriteLine(cs.ExtendedProperties["CS_Default"].Value);
6 }
7}
8%>
2foreach(ColumnSchema cs in SourceTable.Columns) {
3if (cs.ExtendedProperties["CS_Default"] !=null)
4 {
5 Response.WriteLine(cs.ExtendedProperties["CS_Default"].Value);
6 }
7}
8%>
11.如何使用SchemaExplorer得到存儲過程的輸入輸出參數
使用CodeSmith提供的CommandSchema對象,它包含需要的輸入輸出參數集合
1Input Parameters:
2<%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
3{
4 Response.Write(ps.Name);
5 Response.Write("\n");
6}
7%>
8
9
10Output Parameters:
11<%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
12{
13 Response.Write(ps.Name);
14 Response.Write("\n");
15}
16%>
InputOutputParameterExample.cst文件源代碼2<%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
3{
4 Response.Write(ps.Name);
5 Response.Write("\n");
6}
7%>
8
9
10Output Parameters:
11<%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
12{
13 Response.Write(ps.Name);
14 Response.Write("\n");
15}
16%>
1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Generates a update stored procedure."%>
3
4<%@ Property Name="SourceProcedure" Type="SchemaExplorer.CommandSchema"
5 Category="Context"
6 Description="The stored procedure to examine"%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12Input Parameters:
13<%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
14{
15 Response.Write(ps.Name);
16 Response.Write("\n");
17}
18%>
19
20
21Output Parameters:
22<%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
23{
24 Response.Write(ps.Name);
25 Response.Write("\n");
26}
27%>http://www.cnblogs.com/Bear-Study-Hard/archive/2005/12/19/300320.html
2 Description="Generates a update stored procedure."%>
3
4<%@ Property Name="SourceProcedure" Type="SchemaExplorer.CommandSchema"
5 Category="Context"
6 Description="The stored procedure to examine"%>
7
8<%@ Assembly Name="SchemaExplorer"%>
9
10<%@ Import Namespace="SchemaExplorer"%>
11
12Input Parameters:
13<%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
14{
15 Response.Write(ps.Name);
16 Response.Write("\n");
17}
18%>
19
20
21Output Parameters:
22<%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
23{
24 Response.Write(ps.Name);
25 Response.Write("\n");
26}
27%>http://www.cnblogs.com/Bear-Study-Hard/archive/2005/12/19/300320.html