一、 工具設(shè)置 [Tools->Options...->Studio->Editor->Enable unicode]將這個(gè)選項(xiàng)勾上,那么CodeSmith就可以顯示和保存中文了。 中加入ResponseEncoding="UTF-8" 的標(biāo)簽。將會(huì)使得生成的文件也支持中文。 [Tools->Options...->Studio->Editor->Convert tab to]去掉這個(gè)的勾選,就是不使用空格來替換Tab。 (一) 模板說明區(qū)域,只有一句話: (二) 屬性設(shè)置區(qū)域 1) String類型參數(shù)聲明: <%@ Property Default="AAA" Optional="True" Category="輸入?yún)?shù)" Description="這是一個(gè)字符串型的參數(shù)" %> 2) Bool類型參數(shù)聲明: <%@ Property Default="True" Optional="False" Category="輸入?yún)?shù)" Description="這是一個(gè)布朗型的參數(shù)" %> 3) DatabaseSchema類型參數(shù)聲明: <%@ Property Category="Context" Description="這是一個(gè)數(shù)據(jù)庫" %> 4) TableSchemaCollection類型參數(shù)聲明: <%@ Property Category="Context" Description="這是一個(gè)數(shù)據(jù)表集合" %> 5) TableSchema類型參數(shù)聲明: <%@ Property Category="Context" Description="這是一個(gè)數(shù)據(jù)表" %> (三) 注冊(cè)模板區(qū)域 <%@ Register Template="B.cst" MergeProperties="False" ExcludeProperties="" %> 這就是將B模板注冊(cè)到A模板中。 (四) 引用聲明區(qū)域 <%@ Assembly %> <%@ Import Namespace="SchemaExplorer" %> 要自己控制輸出文件的話就需要添加:<%@ Import Namespace="System.IO" %> (五) 模板區(qū)域 直接輸出值為<%= ThisIsString %> 調(diào)用代碼為<% if (ThisIsBool) { %>A<% } %> 如果ThisIsBool為true則輸出A。 (六) 函數(shù)區(qū)域 三、 模板編寫方法 B. 變量輸出 再例如輸出ThisIsTable的名字:<%= ThisIsTable.Name %> C. 調(diào)用函數(shù) <% if (ThisIsBool) { %>A<% } %> D. 調(diào)用模板 <% for(int i = 0; i < ThisIsTableList.Count; i++) { B b = new B(); b.ThisIsTable = ThisIsTableList[i]; b.Render(this.Response); } %> E. 遍歷Database或TableCollection內(nèi)的表 遍歷ThisIsDatabase并輸出表名 <% for (int t = 0; t < ThisIsDatabase.Tables.Count; t++) { %> <%= ThisIsDatabase.Tables[t].Name %> <% } %> F. 遍歷Table的列 //數(shù)據(jù)庫類型:DbType.int private int _ID; 這里調(diào)用了一個(gè)方法DataType2CSharpType(System.Data.DbType dbType)在后面將會(huì)講到。 <% for (int c = 0; c < ThisIsTable.Columns.Count; c++) { %> //數(shù)據(jù)庫類型:DbType.<%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> private <%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> _<%= ThisIsTable.Columns[c].Name %>;
<% } %> 輸出結(jié)果: //數(shù)據(jù)庫類型:DbType.int private int _ID;
//數(shù)據(jù)庫類型:DbType.int private int _ClassID;
//數(shù)據(jù)庫類型:DbType.string private string _StudentName;
G. 遍歷Table的PK 主鍵<%= c %>:<%= ThisIsTable.PrimaryKey.Name %> <%= ThisIsTable.PrimaryKey.Table.Name %>.<%= ThisIsTable.PrimaryKey.MemberColumns[c].Name %> <% } %> 輸出結(jié)果 : 主鍵0:PK_Student Student.ID
H. 遍歷Table的FK(Table自己是外鍵表<即Table為明細(xì)表>) <% for (int c = 0; c < ThisIsTable.ForeignKeys.Count; c++) { %> 外鍵<%= c %>:<%= ThisIsTable.ForeignKeys[c].Name %> 外鍵<%= c %>對(duì)應(yīng)的列 <% for (int i = 0; i < ThisIsTable.ForeignKeys[c].PrimaryKeyMemberColumns.Count; i++) { %> <%= ThisIsTable.ForeignKeys[c].ForeignKeyTable.Name %>.<%= ThisIsTable.ForeignKeys[c].ForeignKeyMemberColumns[0].Name %> <——來自于 <%= ThisIsTable.ForeignKeys[c].PrimaryKeyTable.Name %>.<%= ThisIsTable.ForeignKeys[c].PrimaryKeyMemberColumns[0].Name %> <% } %> <% } %> 輸出結(jié)果: 外鍵0:FK_Student_Class 外鍵0對(duì)應(yīng)的列
I. 遍歷Table的FK(Table自己是主鍵表<即Table為父表>) 其他表外鍵<%= c %>:<%= ThisIsTable.PrimaryKeys[c].Name %> 其他表外鍵<%= c %>對(duì)應(yīng)的列: <% for (int i = 0; i < ThisIsTable.PrimaryKeys[c].PrimaryKeyMemberColumns.Count; i++) { %> <%= ThisIsTable.PrimaryKeys[c].PrimaryKeyTable.Name %>.<%= ThisIsTable.PrimaryKeys[c].PrimaryKeyMemberColumns[0].Name %> 作用于——> <%= ThisIsTable.PrimaryKeys[c].ForeignKeyTable.Name %>.<%= ThisIsTable.PrimaryKeys[c].ForeignKeyMemberColumns[0].Name %> <% } %> <% } %> 輸出結(jié)果: 其他表外鍵0:FK_ExamScore_Student 其他表外鍵0對(duì)應(yīng)的列: Student.ID 作用于——> ExamScore.StudentID 四、 函數(shù)區(qū)域用法 1) 添加一個(gè)選擇目錄的輸入?yún)?shù) private string templateOutputDirectory = "";
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Optional, NotChecked] [Category("OutputInfo")] [Description("輸出結(jié)果的目錄。")] [DefaultValue("")] public string OutputDirectory { get { if (string.IsNullOrEmpty(templateOutputDirectory)) { return "C://"+ (ThisIsDatabase!= null ? ThisIsDatabase.Name : "Output"); } else { return templateOutputDirectory; } } set { if (value.EndsWith("http://")) value = value.Substring(0, value.Length - 1); templateOutputDirectory = value; } } 2) 添加一個(gè)選擇文件的輸入?yún)?shù) private string templateOutputFile;
[Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Optional, NotChecked] [Category("OutputInfo")] [Description("輸出文件")] [DefaultValue("")] public string OutputFile { get { if (string.IsNullOrEmpty(templateOutputFile)) { return "C://"+ (ThisIsDatabase != null ? ThisIsDatabase.Name + ".cs" : "Output.cs"); } else { return templateOutputFile; } } set { templateOutputFile = value; } } 3) 將數(shù)據(jù)庫類型轉(zhuǎn)化為C#類型的函數(shù) public string DataType2CSharpType(System.Data.DbType dbType) { switch (dbType) { case DbType.AnsiString: return "string"; case DbType.AnsiStringFixedLength: return "string"; case DbType.Binary: return "byte[]"; case DbType.Boolean: return "bool"; case DbType.Byte: return "byte"; case DbType.Currency: return "decimal"; case DbType.Date: return "DateTime"; case DbType.DateTime: return "DateTime"; case DbType.DateTime2: return "DateTime"; case DbType.DateTimeOffset: return "DateTime"; case DbType.Decimal: return "decimal"; case DbType.Double: return "double"; case DbType.Guid: return "Guid"; case DbType.Int16: return "short"; case DbType.Int32: return "int"; case DbType.Int64: return "long"; case DbType.Object: return "object"; case DbType.SByte: return "sbyte"; case DbType.Single: return "float"; case DbType.String: return "string"; case DbType.StringFixedLength: return "string"; case DbType.Time: return "DateTime";
case DbType.UInt16: return "ushort"; case DbType.UInt32: return "uint"; case DbType.UInt64: return "ulong"; case DbType.VarNumeric: return "decimal"; case DbType.Xml: return "string"; default: return "object"; } } 4) 獲取數(shù)據(jù)庫類型的字段在C#中的默認(rèn)值 public string DataTypeDefaultValue(System.Data.DbType dbType) { switch (dbType) { case DbType.AnsiString: return "String.Empty"; case DbType.AnsiStringFixedLength: return "String.Empty"; case DbType.Binary: //Answer modified was just 0 return "new byte[] {}"; case DbType.Boolean: return "false"; case DbType.Byte: //Answer modified was just 0 return "(byte)0"; case DbType.Currency: return "0"; case DbType.Date: return "DateTime.MinValue"; case DbType.DateTime: return "DateTime.MinValue"; case DbType.DateTime2: return "DateTime.MinValue"; case DbType.DateTimeOffset: return "DateTime.MinValue"; case DbType.Decimal: return "0.0m"; case DbType.Double: return "0.0f"; case DbType.Guid: return "Guid.Empty"; case DbType.Int16: return "(short)0"; case DbType.Int32: return "(int)0"; case DbType.Int64: return "(long)0"; case DbType.Object: return "new object()"; case DbType.SByte: return "(sbyte)0"; case DbType.Single: return "0F"; case DbType.String: return "String.Empty"; case DbType.StringFixedLength: return "String.Empty"; case DbType.Time: return "new DateTime(1900,1,1,0,0,0,0)"; //return "DateTime.MaxValue"; case DbType.UInt16: return "(ushort)0"; case DbType.UInt32: return "(uint)0"; case DbType.UInt64: return "(ulong)0"; case DbType.VarNumeric: return "(decimal)0"; case DbType.Xml: return "String.Empty"; default: return "null"; } } 5) 文件輸出函數(shù) public override void Render(TextWriter writer) { if (!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); StreamWriter BaseFile = new StreamWriter(OutputFile, false); base.Render(writer); BaseFile.Close(); } 當(dāng)然了,我們也可以再嵌入的其他模板里面調(diào)用這些輸出的方法,從而達(dá)到輸出多個(gè)文件的目的,這里就不再詳細(xì)的寫代碼了。 另附上完整的B的代碼: <%@ CodeTemplate ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="這里是模板說明" %> <%@ Property Category="Context" Description="這是一個(gè)數(shù)據(jù)表" %>
<%@ Assembly %> <%@ Import Namespace="SchemaExplorer" %>
數(shù)據(jù)表名稱:<%= ThisIsTable.Name %>
<% for (int c = 0; c < ThisIsTable.PrimaryKey.MemberColumns.Count; c++) { %> 主鍵<%= c %>:<%= ThisIsTable.PrimaryKey.Name %> <%= ThisIsTable.PrimaryKey.Table.Name %>.<%= ThisIsTable.PrimaryKey.MemberColumns[c].Name %> <% } %>
<% for (int c = 0; c < ThisIsTable.ForeignKeys.Count; c++) { %> 外鍵<%= c %>:<%= ThisIsTable.ForeignKeys[c].Name %> 外鍵<%= c %>對(duì)應(yīng)的列 <% for (int i = 0; i < ThisIsTable.ForeignKeys[c].PrimaryKeyMemberColumns.Count; i++) { %> <%= ThisIsTable.ForeignKeys[c].ForeignKeyTable.Name %>.<%= ThisIsTable.ForeignKeys[c].ForeignKeyMemberColumns[0].Name %> <——來自于 <%= ThisIsTable.ForeignKeys[c].PrimaryKeyTable.Name %>.<%= ThisIsTable.ForeignKeys[c].PrimaryKeyMemberColumns[0].Name %> <% } %> <% } %>
<% for (int c = 0; c < ThisIsTable.PrimaryKeys.Count; c++) { %> 其他表外鍵<%= c %>:<%= ThisIsTable.PrimaryKeys[c].Name %> 其他表外鍵<%= c %>對(duì)應(yīng)的列: <% for (int i = 0; i < ThisIsTable.PrimaryKeys[c].PrimaryKeyMemberColumns.Count; i++) { %> <%= ThisIsTable.PrimaryKeys[c].PrimaryKeyTable.Name %>.<%= ThisIsTable.PrimaryKeys[c].PrimaryKeyMemberColumns[0].Name %> 作用于——> <%= ThisIsTable.PrimaryKeys[c].ForeignKeyTable.Name %>.<%= ThisIsTable.PrimaryKeys[c].ForeignKeyMemberColumns[0].Name %> <% } %> <% } %>
數(shù)據(jù)表Select語句:private const String SelectString = @" SELECT <% for (int c = 0; c < ThisIsTable.Columns.Count; c++) { %> [<%= ThisIsTable.Columns[c].Name %>]<% if (c < ThisIsTable.Columns.Count - 1) { %>,<% } %> <% } %> FROM [<%= ThisIsTable.Name %>] WHERE 1 = 1 ";
各字段數(shù)據(jù)類型: <% for (int c = 0; c < ThisIsTable.Columns.Count; c++) { %> //數(shù)據(jù)庫類型:DbType.<%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> private <%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> _<%= ThisIsTable.Columns[c].Name %>;
<% } %>
<script runat="template"> //將數(shù)據(jù)庫類型轉(zhuǎn)化為C#類型 public string DataType2CSharpType(System.Data.DbType dbType) { switch (dbType) { case DbType.AnsiString: return "string"; case DbType.AnsiStringFixedLength: return "string"; case DbType.Binary: return "byte[]"; case DbType.Boolean: return "bool"; case DbType.Byte: return "byte"; case DbType.Currency: return "decimal"; case DbType.Date: return "DateTime"; case DbType.DateTime: return "DateTime"; case DbType.DateTime2: return "DateTime"; case DbType.DateTimeOffset: return "DateTime"; case DbType.Decimal: return "decimal"; case DbType.Double: return "double"; case DbType.Guid: return "Guid"; case DbType.Int16: return "short"; case DbType.Int32: return "int"; case DbType.Int64: return "long"; case DbType.Object: return "object"; case DbType.SByte: return "sbyte"; case DbType.Single: return "float"; case DbType.String: return "string"; case DbType.StringFixedLength: return "string"; case DbType.Time: return "TimeSpan"; case DbType.UInt16: return "ushort"; case DbType.UInt32: return "uint"; case DbType.UInt64: return "ulong"; case DbType.VarNumeric: return "decimal"; case DbType.Xml: return "string"; default: return "object"; } } </script> |
|