本文來自:http://blog.okbase.net/haobao/archive/32.html

          表格名稱

          <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="1. Context" Description="Table that the documentation should be based on." %>

          <%=SourceTable%>

          不含dbowner的表格名稱

          <%=SourceTable.Name%>

          將表中所有列名作為參數

          void myfunction(<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %><%=CSharpAlias[SourceTable.Columns[i].SystemType.FullName]%> <%=SourceTable.Columns[i].Name%><% if (i < SourceTable.Columns.Count-1) { %>, <% } %><% } %>)

          主鍵列名

          SourceTable.PrimaryKey.MemberColumns[0].Name

          主鍵的SQL數據類型

          SourceTable.PrimaryKey.MemberColumns[0].NativeType

          列名

          SourceTable.Columns[i].Name

          列的數據長度

          SourceTable.Columns[i].Size

          列的系統數據類型

          SourceTable.Columns[i].SystemType.FullName

          Sql本地數據類型轉換到SqlDbType類型

          <%@ Map Name="SqlNativeSqlDb" Src="SqlNativeType-SqlDbType" Description="SqlNativeType to SqlDbType Map" %>

          public string GetSqlDbType(ColumnSchema column)

          {

          return SqlNativeSqlDb[column.NativeType.ToString()];

          }

          自己寫的方法:

          GetSqlDbType(SourceTable.Columns[i])

          public string GetSqlDbType(ColumnSchema column)

          {

          switch (column.NativeType.ToLower())

          {

          case "bigint": return "SqlDbType.BigInt";

          case "binary": return "SqlDbType.Binary";

          case "bit": return "SqlDbType.Bit";

          case "char": return "SqlDbType.Char";

          case "datetime": return "SqlDbType.DateTime";

          case "decimal": return "SqlDbType.Decimal";

          case "float": return "SqlDbType.Float";

          case "image": return "SqlDbType.Image";

          case "int": return "SqlDbType.Int";

          case "money": return "SqlDbType.Money";

          case "nchar": return "SqlDbType.NChar";

          case "ntext": return "SqlDbType.NText";

          case "numeric": return "SqlDbType.Decimal";

          case "nvarchar": return "SqlDbType.NVarChar";

          case "real": return "SqlDbType.Real";

          case "smalldatetime": return "SqlDbType.SmallDateTime";

          case "smallint": return "SqlDbType.SmallInt";

          case "smallmoney": return "SqlDbType.SmallMoney";

          case "sql_variant": return "SqlDbType.Variant";

          case "sysname": return "SqlDbType.NChar";

          case "text": return "SqlDbType.Text";

          case "timestamp": return "SqlDbType.Timestamp";

          case "tinyint": return "SqlDbType.TinyInt";

          case "uniqueidentifier": return "SqlDbType.UniqueIdentifier";

          case "varbinary": return "SqlDbType.VarBinary";

          case "varchar": return "SqlDbType.VarChar";

          default: return "__UNKNOWN__" + column.NativeType;

          }

          }

          系統數據類型轉換為C#數據類型

          <%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>

          CSharpAlias[SourceTable.Columns[i].SystemType.FullName]

          SQL Server數據類型轉換到C#數據類型

          <%@ Map Name="SqlCSharp" Src="Sql-CSharp" Description="SQL Server data type to C# data type map" %>

          public string GetCSharpType(ColumnSchema column)

          {

          return SqlCSharp[column.NativeType.ToString()];

          }

          判斷是否為自增長列

          if( ((bool)SourceTable.Columns[i].ExtendedProperties["CS_IsIdentity"].Value) == true)

          {

          Response.Write(SourceTable.Columns[i].Name);

          }

          讓codesmith支持中文

          1)Tools->Options...->Studio->Editor->Enable unicode support將這個選項勾上。

          2)在模板文件的CodeTemplate聲明中加上ResponseEncoding="UTF-8 "屬性:

          <%@ CodeTemplate ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>

           

          其它參考信息:

          /r/n   /r是換行 /n是回車換行

          SchemaExplorer.TableSchema類:是CodeSmith中自帶的一個類,它描述表的結構。

          Name屬性:表的名稱

          Columns屬性:表中列的集合

          ExtendedProperties屬性:表中指定列的擴展信息

          ForeignKeyColumns屬性:表的一個外鍵列的集合

          ForeignKeys屬性:表的外鍵集合

          HasPrimaryKey屬性:返回表中是否存在主鍵

          NonForeignKeyColumns屬性:表中非外鍵列的集合

          NonKeyColumns屬性:表中非外鍵,非主鍵列的集合

          NonPrimaryKeyColumns屬性:表中非主鍵列的集合

          PrimaryKey屬性:表中的主鍵

          表中指定列的擴展屬性 (ExtendedProperties屬性):

            CS_IsIdentity:自增長列

            CS_IsComputed :計算列

            CS_IdentitySeed :自增長列的起始值

            CS_IdentityIncrement :自增長列的步長

            CS_Default :列的默認值

          posted on 2012-12-23 15:12 sanmao 閱讀(937) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 水富县| 沽源县| 五家渠市| 新巴尔虎左旗| 阳谷县| 和林格尔县| 定襄县| 清远市| 读书| 武宁县| 闻喜县| 嘉荫县| 丰都县| 玉树县| 东港市| 广昌县| 阳谷县| 恩施市| 偃师市| 平邑县| 东港市| 调兵山市| 沈阳市| 鸡泽县| 延川县| 兴安县| 方城县| 赤峰市| 清苑县| 永福县| 无棣县| 河北区| 绥宁县| 克什克腾旗| 印江| 梁山县| 桑日县| 汉川市| 宣威市| 永吉县| 宝山区|