codesmith使用
安装后
破解
将两个dll烤到安装目录下!
自己制作CodeTemplateRule
将CodeSmith.Engine(在bin目录里)和SchemaExplorer(在AddIns目录里) 两个类添加到引用
using System;
using System.Text;
using System.ComponentModel;
using CodeSmith.Engine;
using System.Data;
using SchemaExplorer;
namespace CodeTemplateRule
{
/**/
/// <summary>
/// CodeSmith自动生成代码所用到的函数
/// </summary>
public class CodeTemplateRule
{
/**/
/// <summary>
/// 获取所有列的集合
/// </summary>
/// <param name="dataBase">数据库Schema</param>
/// <param name="tableName">表名</param>
/// <returns>Columns Schema</returns>
public ColumnSchemaCollection GetColumnCollection(DatabaseSchema dataBase, string tableName)
{
TableSchemaCollection tables = new TableSchemaCollection(dataBase.Tables);
ColumnSchemaCollection columns = null;
for (int i = 0; i < tables.Count; i++)
{
if (tables[i].Name.ToUpper() == tableName.ToUpper())
{
TableSchema ts = tables[i];
columns = new ColumnSchemaCollection(ts.Columns);
}
}
return columns;
}
/**/
/// <summary>
/// 获取处理后的表名 例如:ORG_Person,返回Person
/// </summary>
/// <param name="tableName">表名</param>
/// <returns>返回处理后的表名</returns>
public string GetTableName(string tableName)
{
int i = tableName.IndexOf("_");
return tableName.Substring(i + 1, tableName.Length - (i + 1));
}
/**/
/// <summary>
/// 获取唯一号的变量名,第一个字母小写
/// </summary>
/// <param name="guidColumn">唯一号列名</param>
/// <returns>唯一号的变量名</returns>
public string GetGuidColumnVar(string guidColumn)
{
string guid;
guid = guidColumn.Substring(0, 1).ToLower() + guidColumn.Substring(1, guidColumn.Length - 1);
return guid;
}
/**/
/// <summary>
/// 获取c#变量类型
/// </summary>
/// <param name="column">列</param>
/// <returns>c#变量类型</returns>
public string GetType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
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 "DateTime";
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";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
/**/
/// <summary>
/// 获取c#变量类型默认值
/// </summary>
/// <param name="column">列</param>
/// <returns>c#变量类型默认值</returns>
public string GetDefaultValue(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "\"\"";
case DbType.AnsiStringFixedLength: return "\"\"";
case DbType.Binary: return "null";
case DbType.Boolean: return "false";
case DbType.Byte: return "0";
case DbType.Currency: return "0";
case DbType.Date: return "DateTime.Parse(\"1900-1-1\")";
case DbType.DateTime: return "DateTime.Parse(\"1900-1-1\")";
case DbType.Decimal: return "0";
case DbType.Double: return "0";
case DbType.Guid: return "Guid.NewGuid().ToString()";
case DbType.Int16: return "0";
case DbType.Int32: return "0";
case DbType.Int64: return "0";
case DbType.Object: return "\"\"";
case DbType.SByte: return "0";
case DbType.Single: return "0";
case DbType.String: return "\"\"";
case DbType.StringFixedLength: return "";
case DbType.Time: return "DateTime.Parse(\"1900-1-1\")";
case DbType.UInt16: return "0";
case DbType.UInt32: return "0";
case DbType.UInt64: return "0";
case DbType.VarNumeric: return "0";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
/**/
/// <summary>
/// 获取c#变量类型默认值
/// </summary>
/// <param name="column">列</param>
/// <returns>c#变量类型默认值</returns>
public string GetConvert(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "Convert.ToString";
case DbType.AnsiStringFixedLength: return "Convert.ToString";
case DbType.Binary: return "Convert.ToByte";
case DbType.Boolean: return "Convert.ToBoolean";
case DbType.Byte: return "Convert.ToInt32";
case DbType.Currency: return "Convert.ToDecimal";
case DbType.Date: return "Convert.DateTime";
case DbType.DateTime: return "Convert.DateTime";
case DbType.Decimal: return "Convert.ToDecimal";
case DbType.Double: return "Convert.ToDouble";
case DbType.Guid: return "Convert.ToString";
case DbType.Int16: return "Convert.ToInt16";
case DbType.Int32: return "Convert.ToInt32";
case DbType.Int64: return "Convert.ToInt64";
case DbType.Object: return "Convert.ToString";
case DbType.SByte: return "Convert.ToByte";
case DbType.Single: return "Convert.ToInt32";
case DbType.String: return "Convert.ToString";
case DbType.StringFixedLength: return "Convert.ToString";
case DbType.Time: return "Convert.DateTime";
case DbType.UInt16: return "Convert.ToUInt16";
case DbType.UInt32: return "Convert.ToUInt32";
case DbType.UInt64: return "Convert.ToUInt64";
case DbType.VarNumeric: return "Convert.ToDecimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
}
}
using System.Text;
using System.ComponentModel;
using CodeSmith.Engine;
using System.Data;
using SchemaExplorer;
namespace CodeTemplateRule
{
/**/
/// <summary>
/// CodeSmith自动生成代码所用到的函数
/// </summary>
public class CodeTemplateRule
{
/**/
/// <summary>
/// 获取所有列的集合
/// </summary>
/// <param name="dataBase">数据库Schema</param>
/// <param name="tableName">表名</param>
/// <returns>Columns Schema</returns>
public ColumnSchemaCollection GetColumnCollection(DatabaseSchema dataBase, string tableName)
{
TableSchemaCollection tables = new TableSchemaCollection(dataBase.Tables);
ColumnSchemaCollection columns = null;
for (int i = 0; i < tables.Count; i++)
{
if (tables[i].Name.ToUpper() == tableName.ToUpper())
{
TableSchema ts = tables[i];
columns = new ColumnSchemaCollection(ts.Columns);
}
}
return columns;
}
/**/
/// <summary>
/// 获取处理后的表名 例如:ORG_Person,返回Person
/// </summary>
/// <param name="tableName">表名</param>
/// <returns>返回处理后的表名</returns>
public string GetTableName(string tableName)
{
int i = tableName.IndexOf("_");
return tableName.Substring(i + 1, tableName.Length - (i + 1));
}
/**/
/// <summary>
/// 获取唯一号的变量名,第一个字母小写
/// </summary>
/// <param name="guidColumn">唯一号列名</param>
/// <returns>唯一号的变量名</returns>
public string GetGuidColumnVar(string guidColumn)
{
string guid;
guid = guidColumn.Substring(0, 1).ToLower() + guidColumn.Substring(1, guidColumn.Length - 1);
return guid;
}
/**/
/// <summary>
/// 获取c#变量类型
/// </summary>
/// <param name="column">列</param>
/// <returns>c#变量类型</returns>
public string GetType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
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 "DateTime";
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";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
/**/
/// <summary>
/// 获取c#变量类型默认值
/// </summary>
/// <param name="column">列</param>
/// <returns>c#变量类型默认值</returns>
public string GetDefaultValue(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "\"\"";
case DbType.AnsiStringFixedLength: return "\"\"";
case DbType.Binary: return "null";
case DbType.Boolean: return "false";
case DbType.Byte: return "0";
case DbType.Currency: return "0";
case DbType.Date: return "DateTime.Parse(\"1900-1-1\")";
case DbType.DateTime: return "DateTime.Parse(\"1900-1-1\")";
case DbType.Decimal: return "0";
case DbType.Double: return "0";
case DbType.Guid: return "Guid.NewGuid().ToString()";
case DbType.Int16: return "0";
case DbType.Int32: return "0";
case DbType.Int64: return "0";
case DbType.Object: return "\"\"";
case DbType.SByte: return "0";
case DbType.Single: return "0";
case DbType.String: return "\"\"";
case DbType.StringFixedLength: return "";
case DbType.Time: return "DateTime.Parse(\"1900-1-1\")";
case DbType.UInt16: return "0";
case DbType.UInt32: return "0";
case DbType.UInt64: return "0";
case DbType.VarNumeric: return "0";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
/**/
/// <summary>
/// 获取c#变量类型默认值
/// </summary>
/// <param name="column">列</param>
/// <returns>c#变量类型默认值</returns>
public string GetConvert(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "Convert.ToString";
case DbType.AnsiStringFixedLength: return "Convert.ToString";
case DbType.Binary: return "Convert.ToByte";
case DbType.Boolean: return "Convert.ToBoolean";
case DbType.Byte: return "Convert.ToInt32";
case DbType.Currency: return "Convert.ToDecimal";
case DbType.Date: return "Convert.DateTime";
case DbType.DateTime: return "Convert.DateTime";
case DbType.Decimal: return "Convert.ToDecimal";
case DbType.Double: return "Convert.ToDouble";
case DbType.Guid: return "Convert.ToString";
case DbType.Int16: return "Convert.ToInt16";
case DbType.Int32: return "Convert.ToInt32";
case DbType.Int64: return "Convert.ToInt64";
case DbType.Object: return "Convert.ToString";
case DbType.SByte: return "Convert.ToByte";
case DbType.Single: return "Convert.ToInt32";
case DbType.String: return "Convert.ToString";
case DbType.StringFixedLength: return "Convert.ToString";
case DbType.Time: return "Convert.DateTime";
case DbType.UInt16: return "Convert.ToUInt16";
case DbType.UInt32: return "Convert.ToUInt32";
case DbType.UInt64: return "Convert.ToUInt64";
case DbType.VarNumeric: return "Convert.ToDecimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
}
}
将 重新编译生成的CodeTemplateRule拷贝到 codesmith目录下的 两个文件夹中
模板页中添加引用
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)