CodeSmith 部分學習 1. 註冊子模板child.cst到主模板 <%@ Register Name="ChildTemplate" Template="child.cst" MergeProperties="False" ExcludeProperties="" %> 2. 調用子模板並輸出 <% for(int i = 0; i < ThisTableList.Count; i++) { CodeTemplate b = new ChildTemplate(); b.ThisTable = ThisTableList[i]; b.Render(this.Response); } %> 3. 遍歷TableCollection表,並輸出表名 <% for (int t = 0; t < ThisDatabase.Tables.Count; t++) { %> <%= ThisDatabase.Tables[t].Name %> <% } %> 4. 遍歷Table列,其中DataType2CSharpType爲一個自定義方法 <% for (int c = 0; c < ThisTable.Columns.Count; c++) { %> //數據庫類型:DbType.<%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> private <%= DataType2CSharpType(ThisIsTable.Columns[c].DataType) %> _<%= ThisIsTable.Columns[c].Name %>; <% } %> 5. 遍歷Table的PK <% for(int i=0;i<ThisTable.PrimaryKey.MemberColumns.Count;i++) {%> //主鍵名<%= i%>:<%= ThisTable.PrimaryKey.Name%> <%= ThisTable.PrimaryKey.Table.Name %>.<%= ThisTable.PrimaryKey.MemberColumns[i].Name %> <%}%> 6. 遍歷Table的FK(從代表細)(以1:1爲例) <% for(int i=0;i<ThisTable.ForeignKeys.Count;i++){%> //外鍵<%= i %>:<%= ThisTable.ForeignKeys[i].Name %> //外鍵<%= i %>對應的列 <% for(int j=0;j<ThisTable.ForeignKeys[i].PrimaryKeyMemberColumns.Count;j++){%> <%= ThisTable.ForeignKeys[i].ForeignKeyTable.Name %>.<%= ThisTable.ForeignKeys[i].ForeignKeyMemberColumns[0].Name %> <%}%> <%}%> 7. 遍歷Table的FK(主鍵表)(以1:1爲例) <% for(int i=0;i<ThisTable.PrimaryKeys.Count;i++){%> //其餘表從鍵<%= i %>:<%= ThisTable.PrimaryKeys[i].Name %> //外鍵<%= i %>對應的列 <% for(int j=0;j<ThisTable.PrimaryKeys[i].PrimaryKeyMemberColumns.Count;j++){%> <%= ThisTable.PrimaryKeys[i].ForeignKeyTable.Name %>.<%= ThisTable.PrimaryKeys[i].ForeignKeyMemberColumns[0].Name %> <%}%> <%}%> 8. 添加選擇目錄輸入參數 private string templateOutputDirectory=""; [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor),typeof(System.Drawing.Design.UITypeEditor))] [Optional,NotChecked] [Category("OutputInfo")] [Description("輸出結果目錄")] [DefaultValue("")] public string OutputDirectory { get{ if(string.IsNullOrEmpty(templateOutputDirectory)) { return "C:\\"+(ThisDatabase!=null?ThisDatabase.Name:"Output"); } else { return templateOutputDirectory; } } set{ if(value.EndsWith("\\") value=value.Substring(0,value.Length-1); templateOutputDirectory=value; } } 9. 添加選擇文件的輸入參數 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:\\"+(ThisDatabase!=null?ThisDatabase.Name+".cs":"Output.cs"); } else{ return templateOutputFile; } } set{ templateOutputFile=value; } } 或者 <script runat="template"> //生成實體Entity類 privatevoid GenerateEntityClasses() { CodeTemplate Template =new EntityTemplate(); foreach(TableSchema table inthis.SourceDatabase.Tables) { string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs"; //生成模板 Template.SetProperty("Table",table); //文件輸出 Template.RenderToFile(FileDirectory,true); Debug.WriteLine(FileDirectory +" 建立成功."); } } </script> 10. 數據庫類型轉換成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 "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"; } } 11. 獲取數據庫類型的字段在C#中的默認值 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"; } } 12. 文件輸出函數 public override void Render(TextWriter writer) { if (!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); StreamWriter BaseFile = new StreamWriter(OutputFile, false); base.Render(writer); BaseFile.Close(); }