CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板

Posted 邢帅杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板相关的知识,希望对你有一定的参考价值。

C#生成sql视图的实体类

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 ViewUtility
{

//get Columns info by TableName
public ViewColumnSchemaCollection GetColumnCollectionByTable(ViewSchema table)
{
ViewColumnSchemaCollection columns = new ViewColumnSchemaCollection(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(ViewSchema table)
{
return GetMemberConstantDeclarationStatement("public const String ", table);
}
//suck as USER_TABLE
public string GetUpperStatement(ViewSchema 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, ViewSchema 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(ViewColumnSchema 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(ViewColumnSchema 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;
}
}
}

 

对应的.cst模板

<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="T-SQL" Description="生成更新视图的存储过程." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.ViewSchema" Category="Context" Description="存储过程操作的视图名称." %>
<%@ Assembly Src="ViewUtility.cs" %>
<%@ Import Namespace="Common.Data" %>
<script runat="template">
public string FormatTable(string param)
{
string[] p =param.Split(‘_‘);
return p[p.Length-1].ToString();
}
public string ReplaceTablePrex(string param)
{
return param.Replace("fw_","");
}
public string Format(string param1)
{
string param="";
switch (param1)
{
case "System.String":
{
param = "Convert.ToString";
break;
}
case "System.DateTime":
{
param = "Convert.ToDateTime";
break;
}
case "System.Decimal":
{
param="Convert.ToDecimal";
break;
}
case "System.Boolean":
{
param="Convert.ToBoolean";
break;
}
default:
{
param ="Convert.ToInt32";
break;
}

}
return param;

}
public string FormatValue(string param1)
{
string param="";
switch (param1)
{
case "System.String":
{
param = "\"\"";
break;
}
case "System.DateTime":
{
param = "DateTime.Now";
break;
}
case "System.Decimal":
{
param="Decimal.Parse(\"0.00\")";
break;
}
case "System.Boolean":
{
param="false";
break;
}
default:
{
param ="0";
break;
}

}
return param;
}
ViewUtility rule=new ViewUtility();
</script>
using System;
using com.enkj.kernel;

namespace com.enkj.SongHe.Model
{
/// <summary>
/// 读取数据库对应的视图 ‘<%= SourceTable.Name %>‘
/// </summary>
[Serializable]
public class <%=FormatTable(SourceTable.Name)%>:IEntity
{
/// <summary>
///表名
/// </summary>
public const string TABLE_NAME = "<%=SourceTable.Name%>";

/// <summary>
/// 表中所有字段集合
/// </summary>
public const string ALL_FILED = @"<%for(int i=0;i<SourceTable.Columns.Count;i++){%><%if(i<SourceTable.Columns.Count-1){%>[<%=SourceTable.Columns[i].Name%>],<%}else{%>[<%=SourceTable.Columns[i].Name%>]<%}%><%}%>";


<%for(int i=0;i<SourceTable.Columns.Count;i++){%>
/// <summary>
/// <%=SourceTable.Columns[i].Description%>字段
/// </summary>
public const string FLD_<%=SourceTable.Columns[i].Name%>="<%=SourceTable.Columns[i].Name%>";
/// <summary>
/// <%=SourceTable.Columns[i].Description%>参数字段
/// </summary>
public const string FAR_<%=SourceTable.Columns[i].Name%>="@<%=SourceTable.Columns[i].Name%>";
private <%=SourceTable.Columns[i].SystemType%> _<%=SourceTable.Columns[i].Name%>=<%=FormatValue(SourceTable.Columns[i].SystemType.ToString())%>;

/// <summary>
/// <%=SourceTable.Columns[i].Description%>
/// </summary>
public <%=SourceTable.Columns[i].SystemType%> <%=SourceTable.Columns[i].Name%>
{
get { return _<%=SourceTable.Columns[i].Name%>; }
set { _<%=SourceTable.Columns[i].Name%> = value; }
}
<%}%>


/// <summary>
/// 无参构造函数
/// </summary>
public <%=FormatTable(SourceTable.Name)%>()
{
}
/// <summary>
/// 通过DataRow实例化一个对象
/// </summary>
/// <param name="_DataRow">行记录</param>
public override void SetDataRow(System.Data.DataRow _DataRow)
{
<%for(int i=0;i<SourceTable.Columns.Count;i++){%>
try
{
if( _DataRow.Table.Columns.Contains(FLD_<%=SourceTable.Columns[i].Name%>))
this.<%=SourceTable.Columns[i].Name%> = <%=Format(SourceTable.Columns[i].SystemType.ToString())%>(_DataRow[FLD_<%=SourceTable.Columns[i].Name%>]);
}
catch(Exception ex)
{
throw new Exception("<%=SourceTable.Name%>::SetDataRow : 语句:this.<%=SourceTable.Columns[i].Name%> = <%=Format(SourceTable.Columns[i].SystemType.ToString())%>(_DataRow[FLD_<%=SourceTable.Columns[i].Name%> ]); 转化失败 ");
}
<%}%>

}
}
}

以上是关于CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板的主要内容,如果未能解决你的问题,请参考以下文章

CodeSmith单表生成实体模板

代码生成工具——CodeSmith

ToolsCodeTemplate使用

CodeSmith生成的增删改查方法模板源码分享

CodeSmith 多模板按目录树批量自动生成代码

动软代码生成器 可用于生成Entity层,可更改模板 /codesmith 也可以