上一篇读取了一个表的内容,但是到了真正应用的时候还是不够用的,我们很容易可以对比出来,SQL里边的数据类型的定义和C#中有所不同,比如Saler--String,大写的String在C#中不是一个类型,所以在这里我们要进行转换一下,还有有的时候我们要加一些操作的逻辑,但这个逻辑不只在一个地方会使用到,这时候我们就可以建一个方法来调用。
在CodeSmith中创建方法要把方法放在<script runat="template">……</script>这个标签中,template我理解为运行在模板中的,所以他可以用来定义方法。如下方式:
<script runat="template">
public string Test()
{
return "测试";
}
</script>
这样就创建了一个Test的方法,调用的时候和之前一样使用<%= Test() %>,需要注意的是,这里括号后边没有分号,<%= Test(); %>是不对的。
下面是一个数据库类型转C#数据类型的方法,这个是从网上搜集来的。
/// <summary> /// 将数据库类型转化为C#类型 /// </summary> /// <param name="dbType">数据库类型</param> /// <returns>C#类型</returns> public string DataTypeToCSharpType(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"; } }
有了它我们就可以像这样调用了
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
public <%= DataTypeToCSharpType(col.DataType) %> <%= col.Name %> { get;set; }
<% } %>
本篇示例模板:
<%--
Name: 测试模板3
Author: GodFinal
Description:
--%>
<%@ Template Language="C#" TargetLanguage="SQL" Description="连接SQL数据库,并读取表结构" Debug="True"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="表" Description="要操作的表" %>
using System;
namespace GodFinal
{
/// <summary>
/// <%=ClassName %>[实体]
/// </summary>
public class <%=ClassName %>
{
<%foreach(ColumnSchema col in SourceTable.Columns){ %>
public <%= DataTypeToCSharpType(col.DataType) %> <%= col.Name %> { get;set; }
<% } %>
}
}
<script runat="template">
/// <summary>
/// 将数据库类型转化为C#类型
/// </summary>
/// <param name="dbType">数据库类型</param>
/// <returns>C#类型</returns>
public string DataTypeToCSharpType(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>
最终结果:
using System;
namespace GodFinal
{
/// <summary>
/// tb_sys_user[实体]
/// </summary>
public class tb_sys_user
{
public int id { get;set; }
public string CPlace { get;set; }
public string Saler { get;set; }
public string Company { get;set; }
public string Department { get;set; }
public string Account { get;set; }
public string telephone { get;set; }
public string email { get;set; }
public string Address { get;set; }
public string Zipcode { get;set; }
public string Level { get;set; }
}
}