打开开始--所有程序---CodeSmith Professional 5.0.1.4983--CodeSmith Explorer
新建立到所要操作数据库的连接名称自定义为DB
打开开始--所有程序---CodeSmith Professional 5.0.1.4983--CodeSmith Studio
新建立一个模板文件BusinessObjctForTable.cst(模板文件可以Build和运行)
新建立一个文件TableUtility.cs(数据库字段到VS2005数据类型映射,命名空间和上面的命名空间保持一致)
COPY下面的代码然后点tools/build/run就完成了数据表实体类文件的生成
注意由模板BusinessObjctForTable.cst生成的"数据表名.cs"实体类文件所属命名空间Common.Model和调用的(TableUtility.cs)类文件的所属命名空间Common.Data的存在与否.在VS2005的工程中必须存在或者按您的需求自行更改。
BusinessObjctForTable.cst代码如下:
<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="T-SQL" Description="生成更新数据表的存储过程." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="存储过程操作的数据表名称." %>
<%@ Assembly Src="TableUtility.cs" %>
<%@ Import Namespace="Common.Data" %>
<script runat="template">TableUtility rule=new TableUtility();</script>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
namespace Common.Model
{
/// <summary>
/// 读取数据库对应的数据表 '<%= SourceTable.Name %>'生成实体类
/// </summary>
[Serializable]
public class <%= SourceTable.Name %>
{
#region Private Member
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
private <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> _<%= SourceTable.Columns[i].Name %>;
<% } %>
#endregion
#region Constructor
public <%= SourceTable.Name %>() {}
public <%= SourceTable.Name %>(
<% for (int i = 0; i < SourceTable.Columns.Count-1; i++) { %>
<%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> <%= SourceTable.Columns[i].Name %>,
<% } %>
<%= rule.GetCSharpVariableType(SourceTable.Columns[SourceTable.Columns.Count-1]) %> <%= SourceTable.Columns[SourceTable.Columns.Count-1].Name %>
)
{
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
_<%= SourceTable.Columns[i].Name %> = <%= SourceTable.Columns[i].Name %>;
<% } %>
}
#endregion
#region Public Properties
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
public <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> <%= rule.GetCamelCaseName(SourceTable.Columns[i].Name) %>
{
get { return _<%= SourceTable.Columns[i].Name %>; }
set { _<%= SourceTable.Columns[i].Name %> = value; }
}
<% } %>
#endregion
}
}
TableUtility.cs代码如下:
using System;
using System.Text;
using CodeSmith.Engine;
using SchemaExplorer;
using System.ComponentModel;
using System.Data;
namespace Common.Data
{
//// <summary>
/// CodeSmith生成SQL Server数据表的实体类调用脚本
/// </summary>
public class TableUtility
{
//get Columns info by TableName
public ColumnSchemaCollection GetColumnCollectionByTable(TableSchema table)
{
ColumnSchemaCollection columns = new ColumnSchemaCollection(table.Columns);
return columns;
}
//Get camelcase name,such as Customer,
public string GetCamelCaseName(string str)
{
return str.Substring(0,1).ToUpper()+str.Substring(1);
}
//Get ,user,private const String USER_FIELD = "User"
public string GetMemberConstantDeclarationStatement(ColumnSchema column)
{
return GetMemberConstantDeclarationStatement("public const String ",column);
}
//such as public const String USER_TABLE = "User"
public string GetTableConstantDeclarationStatement(TableSchema table)
{
return GetMemberConstantDeclarationStatement("public const String ",table);
}
//suck as USER_TABLE
public string GetUpperStatement(TableSchema table)
{
return table.Name.ToUpper()+"_TABLE";
}
//suck as USER_FIELD
public string GetUpperStatement(ColumnSchema column)
{
return column.Name.ToUpper()+"_FIELD";
}
// such as USER_TABLE = "User"
public string GetMemberConstantDeclarationStatement(string protectionLevel, TableSchema table)
{
return protectionLevel+GetUpperStatement(table)+" = "+GetCamelCaseName(table.Name)+"";
}
//such as USERID_FIELD = "Userid"
public string GetMemberConstantDeclarationStatement(string protectionLevel,ColumnSchema column)
{
return protectionLevel+GetUpperStatement(column)+" = "+GetCamelCaseName(column.Name)+"";
}
public string GetCSharpVariableType(ColumnSchema column)
{
switch(column.DataType)
{
case DbType.AnsiString: return "string";
case DbType.AnsiStringFixedLength: return "string";
case DbType.Binary: return "byte[]";
case DbType.Boolean: return "bool";
case DbType.Byte: return "int";
case DbType.Currency: return "decimal";
case DbType.Date: return "DataTime";
case DbType.DateTime: 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";
}
return null;
}
public string GetCSharpBaseType(ColumnSchema column)
{
switch(column.DataType)
{
case DbType.AnsiString: return "System.String";
case DbType.AnsiStringFixedLength: return "System.String";
case DbType.Binary: return "System.Byte[]";
case DbType.Boolean: return "System.Boolean";
case DbType.Byte: return "System.Int32";
case DbType.Currency: return "System.Decimal";
case DbType.Date: return "System.DataTime";
case DbType.DateTime: return "System.DataTime";
case DbType.Decimal: return "System.Decimal";
case DbType.Double: return "System.Double";
case DbType.Guid: return "System.Guid";
case DbType.Int16: return "System.Int16";
case DbType.Int32: return "System.Int32";
case DbType.Int64: return "System.Int64";
case DbType.Object: return "System.Object";
case DbType.SByte: return "System.SByte";
case DbType.Single: return "System.Single";
case DbType.String: return "System.String";
case DbType.StringFixedLength: return "System.String";
case DbType.Time: return "System.TimeSpan";
case DbType.UInt16: return "System.UInt16";
case DbType.UInt32: return "System.UInt32";
case DbType.UInt64: return "System.UInt64";
case DbType.VarNumeric: return "System.Decimal";
}
return null;
}
}
}