这里使用的是动软的模板.
这是动软代码生成器的一个整体界面。
下面做的示例是从右边模板管理中的选一个模板进行修改,这里我选了简单三层模板中的DAL.cmt模板
<#@ 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 DAL <# if( host.Folder.Length > 0){ #>.<#= host.Folder #>
<# } #>
{ <# if( host.TableDescription.Length > 0) {#> //<#= host.TableDescription #><# } #>
public partial class <#= host.GetDALClass(host.TableName) #>DAO
{上面的代码也是模板中我们非常熟悉的引用,接下来我来说下我今天要修改的内容。UPDATE功能,动软中的update功能是将一个实体传进去,全部修改,这里我把它修改成,只修改实体中存在的值
/// <summary>/// this is a new update module/// </summary>public void Update(<#= ModelSpace #>Entity model)
{ StringBuilder strSql=new StringBuilder(); List<SqlParameter> parameters = new List<SqlParameter>();}
先来定义两个变量,因为和实际代码中是一样的,所以这里只有一点,就是<# = ModelSpace#>,这个变量已经在文件的开头定义了,就是该模板的空间名+类名。
接下来我们需要写一个for循环,来判断哪些代码要添加的sql语句中,哪些代码不需要。
strSql.Append("update <#= host.TableName #> set ");<# for(int i=0;i< host.Fieldlist.Count;i++)
{ ColumnInfo c = host.Fieldlist[i]; #><# if (!c.IsIdentity) {#> if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#> ");}<# }#><# }#>
这里的几个变量解释写
| <#= host.TableName #> | 表名 |
| host.Fieldlist.Count | 字段数 |
| c.IsIdentity | 是否主键 |
| <#=preParameter#> | @符号 |
| <#=c.ColumnName#> | 列名 |
.
最后加一个条件语句
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> ");现在sql语句写好了,当时发现没有写变量;添加变量比较简单。只要在每个判空的条件语句后面添加就可以了
<# if (!c.IsIdentity) {#> if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}<# }#>
<# }#>
添加之后,会发现所有实体的属性都被用string.IsNullOrEmpty()来判断,所以这里还要添加一个判断属性类型的条件
<# if (!c.IsIdentity) {#> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#>
if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}<# }#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#>if(model.<#=c.ColumnName#>!=0){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}
<#}#>
<#}#>
上面是个例子,具体情况具体分析。
最后整体的UPDATE代码如下
/// <summary> /// this is a new update module l||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| /// </summary>public bool Update(<#= ModelSpace #>Entity model)
{ StringBuilder strSql=new StringBuilder(); List<SqlParameter> parameters = new List<SqlParameter>(); strSql.Append("update <#= host.TableName #> set ");<# for(int i=0;i< host.Fieldlist.Count;i++)
{ ColumnInfo c = host.Fieldlist[i]; #> <# if (!c.IsIdentity) {#> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#>
if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}<# }#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
<# }#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#> if(model.<#=c.ColumnName#>!=0){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}
<#}#>
<#}#>
<# }#>
strSql = strSql.Remove(strSql.Length - 2,2);
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> ");<# for(int i=0;i< host.Keys.Count;i++)
{ ColumnInfo key = host.Keys[i]; #> <# if (key.IsPrimaryKey || !key.IsIdentity) {#>parameters.Add(new SqlParameter("<#=preParameter#><#=key.ColumnName#>",model.<#=key.ColumnName#>));
<#}#>
<# }#>
int rows=<#= host.DbHelperName#>.ExecuteSql(strSql.ToString(),parameters.ToArray()); if (rows > 0) {return true;
}
else {return false;
}
}
