动软代码啊生成器

Model层自定义模版生成  自己常用结构

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
#>
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
namespace <#= host.NameSpace #>.Model<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #>
{
<# if( host.TableDescription.Length > 0) {#>
//<#= host.TableDescription #>
<# } #>
public class <#= host.GetModelClass(host.TableName) #>
{
<# foreach (ColumnInfo c in host.Fieldlist)
{ #>/// <summary>
/// <#= string.IsNullOrEmpty(c.Description) ? c.ColumnName : c.Description #>
/// </summary>
public <#= CodeCommon.DbTypeToCS(c.TypeName) #> <#= c.ColumnName #>{get;set;}
<# } #>

public <#= host.GetModelClass(host.TableName) #>()
{
<# foreach (ColumnInfo d in host.Fieldlist)
{ #>
<#= d.ColumnName #> = "";
<# } #>
}
}
}

Dal层

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
string DbParaHead=host.DbParaHead;
string DbParaDbType=host.DbParaDbType;
string preParameter=host.preParameter;
string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
ColumnInfo identityKey=host.IdentityKey;
string returnValue = "void";
if (identityKey!=null)
{
returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);
}
#>
using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Maticsoft.DBUtility;
namespace <#= host.NameSpace #>.DAL
<# if( host.Folder.Length > 0){ #>
.<#= host.Folder #>
<# } #>
{
<# if( host.TableDescription.Length > 0) {#>
//<#= host.TableDescription #>
<# } #>
public class <#= host.GetDALClass(host.TableName) #>
{
public bool Exists(string ID)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(1) from <#= host.TableName #>");
strSql.Append(" where ID=@ID");
SqlParameter[] parameters = {
new SqlParameter("@ID",model.ID)
};
int results = _access.exsit(strsql.ToString(),parameters);
return results > 0;
}


/// <summary>
/// 增加一条数据
/// </summary>
public <#= returnValue #> Add(<#= ModelSpace #> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into <#= host.TableName #>(");
strSql.Append("<# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; if (!c.IsIdentity) {#><#= c.ColumnName#><# if (i< host.Fieldlist.Count-1 ) {#>,<#}#><#}}#>");
strSql.Append(") values (");
strSql.Append("<# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; if (!c.IsIdentity) {#>@<#= c.ColumnName#><# if (i< host.Fieldlist.Count-1 ) {#>,<#}#><#}}#>");
strSql.Append(");");

SqlParameter[] parameters = {
<# for(int i=0;i< host.Fieldlist.Count;i++)
{
ColumnInfo c = host.Fieldlist[i];
if(c.IsIdentity) continue;
#>
new SqlParameter("@<#=c.ColumnName#>",<# if ("uniqueidentifier" == c.TypeName.ToLower()){#>Guid.NewGuid();<#} else {#>model.<#=c.ColumnName#> <#} n=n+1; #>) <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#>
<# }#>
};
int results = _access.execCommand(strsql.ToString(),parameters);
return results > 0;
}


/// <summary>
/// 更新一条数据
/// </summary>
public bool Update(<#= ModelSpace #> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update <#= host.TableName #> set ");
<# for(int i=0;i< host.Fieldlist.Count;i++)
{ ColumnInfo c = host.Fieldlist[i]; #>
<# if (!c.IsIdentity) {#>
strSql.Append(" <#= c.ColumnName #> = @<#=c.ColumnName#> <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#> ");<# }#>
<# }#>
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,"@") #> ");

SqlParameter[] parameters = {
<# for(int i=0;i< host.Fieldlist.Count;i++)
{ ColumnInfo c = host.Fieldlist[i]; #>
new SqlParameter("@<#=c.ColumnName#>",model.<#=c.ColumnName#>)

<# }#>
};
int results = _access.execCommand(strsql.ToString(),parameters);
return results > 0;
}


/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete(<#= ModelSpace #> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("delete from <#= host.TableName #> ");
strSql.Append(" where ID=@ID");
SqlParameter[] parameters = {
new SqlParameter("@ID",model.ID)
};
int results = _access.execCommand(strsql.ToString(),parameters);
return results > 0;
}

/// <summary>
/// 得到一个对象实体
/// </summary>

public <#= ModelSpace #> GetTEntityById(string primaryKeyId)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select * ");
strSql.Append(" from <#= host.TableName #> ");
strSql.Append(" where ID=@ID");
SqlParameter[] parameters = {
new SqlParameter("@ID",primaryKeyId)
};
return _access.getSinggleObj<<#= ModelSpace #>>(sb.ToString(),parameters);
}


/// <summary>
/// 获得数据列表
/// </summary>
public getDataTable GetList(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM <#= host.TableName #> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
return _access.getDataTable(strSql.ToString());
}

/// <summary>
/// 获得前几行数据
/// </summary>
public DataTable GetList(int Top,string strWhere,string filedOrder)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select ");
if(Top>0)
{
strSql.Append(" top "+Top.ToString());
}
strSql.Append(" * ");
strSql.Append(" FROM <#= host.TableName #> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
strSql.Append(" order by " + filedOrder);
return _access.getDataTable(strSql.ToString());
}
}
}

<#+
int n=0;
#>

原文地址:https://www.cnblogs.com/sdya/p/8582768.html