CodeSmith DAL层的写法

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Collections" %>
<script runat="template">
// My methods here.

public string GetAllColStr(string flag)
{
string colStr="";
for(int i=0;i<SourceTable.Columns.Count;i++)
{
string col_name=SourceTable.Columns[i].Name;
if(i==SourceTable.Columns.Count-1)
{
colStr +=flag+col_name+"";
}
else
{
colStr +=flag+col_name+",";
}
}
return colStr;
}

public string GetSqlPara()
{
string colStr="";
for(int i=0;i<SourceTable.Columns.Count;i++)
{
string col_name=SourceTable.Columns[i].Name;
string col_type=SourceTable.Columns[i].SystemType.Name;
//string col_type=SourceTable.Columns[i].DataType.v.Name;
if(col_type=="String")
{
col_type="VarChar";
}
else if(col_type=="Int32")
{
col_type="Int";
}
else if(col_type=="Decimal")
{
col_type="Decimal";
}

string col_size=SourceTable.Columns[i].Size.ToString();
colStr += "new SqlParameter(/"@"+col_name+"/",SqlDbType."
+col_type+","+col_size+"),"+System.Environment.NewLine;
}
colStr += "};"+System.Environment.NewLine;
for(int i=0;i<SourceTable.Columns.Count;i++)
{
string col_name=SourceTable.Columns[i].Name;
string col_type=SourceTable.Columns[i].SystemType.Name;
string col_size=SourceTable.Columns[i].Size.ToString();
colStr += "parameters["+i.ToString()+"].Value = model."
+col_name+";"+System.Environment.NewLine;
}
return colStr+"";
}
public string GetUpdateStr()
{
string colStr="";
//strSql.Append("od_rate3=@od_rate3,");
for(int i=0;i<SourceTable.Columns.Count;i++)
{
string col_name=SourceTable.Columns[i].Name;
if(i==SourceTable.Columns.Count-1)
{
colStr += "strSql.Append(/""+col_name+"=@"+col_name+"/");"
+System.Environment.NewLine;
}
else
{
colStr += "strSql.Append(/""+col_name+"=@"+col_name+",/");"
+System.Environment.NewLine;
}

}
return colStr;
}

public string GetModelStr()
{
string colStr="";
for(int i=0;i<SourceTable.Columns.Count;i++)
{
string col_name=SourceTable.Columns[i].Name;
string col_type=SourceTable.Columns[i].SystemType.Name;
string col_size=SourceTable.Columns[i].Size.ToString();
if(col_type=="String")
{
colStr +="model."+col_name+"=ds.Tables[0].Rows[0][/""+col_name+"/"].ToString();"
+System.Environment.NewLine;
}
else
{
colStr+="if(ds.Tables[0].Rows[0][/""+col_name+"/"].ToString()!=/"/")"
+System.Environment.NewLine;

colStr+="{"+System.Environment.NewLine;
colStr+="model."+col_name+"="+col_type+".Parse(ds.Tables[0].Rows[0][/""+col_name+"/"].ToString());"
+System.Environment.NewLine;

colStr+="}"+System.Environment.NewLine;
}
}
return colStr;
}

</script>
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using NNERP.Common;

namespace NNERP.Dal
{
/// <summary>
/// Dal Class <%=SourceTable.Name%>
/// </summary>
public class <%=SourceTable.Name%>: MarshalByRefObject, I<%=SourceTable.Name%>
{
public <%=SourceTable.Name%>()
{}

#region Method

/// <summary>
/// Get Max quantiry from <%=SourceTable.Name%>
/// </summary>
/// <returns></returns>
public int GetCount()
{
return DbHelperSQL.GetCount("<%=SourceTable.Name%>");
}


/// <summary>
/// Get Max ID from <%=SourceTable.Name%>
/// </summary>
public int GetMaxId()
{
return DbHelperSQL.GetMaxID("uid", "<%=SourceTable.Name%>");
}
/// <summary>
/// Exists the Record
/// </summary>
public bool Exists(string uid)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(1) from <%=SourceTable.Name%>");
strSql.Append(" where uid= @uid");
SqlParameter[] parameters = {
new SqlParameter("@uid", SqlDbType.VarChar)
};
parameters[0].Value = uid;
return DbHelperSQL.Exists(strSql.ToString(),parameters);
}


/// <summary>
/// Add a Record
/// </summary>
public int Add(NNERP.Model.<%=SourceTable.Name%> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into <%=SourceTable.Name%>(");
strSql.Append("<%=GetAllColStr("")%>)");
strSql.Append(" values (");
strSql.Append("<%=GetAllColStr("@")%>)");
SqlParameter[] parameters = {
<%=GetSqlPara()%>

parameters = NNERP.Common.DbHelperSQL.ConvertSqlParaNULL(parameters);
DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
return int.Parse(model.uid.ToString());

}
/// <summary>
/// Update a Record
/// </summary>
public void Update(NNERP.Model.<%=SourceTable.Name%> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update <%=SourceTable.Name%> set ");
<%=GetUpdateStr()%>
strSql.Append(" where uid=@uid");
SqlParameter[] parameters = {
<%=GetSqlPara()%>

parameters = NNERP.Common.DbHelperSQL.ConvertSqlParaNULL(parameters);
DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
}

/// <summary>
/// Delete a Record
/// </summary>
public void Delete(string uid)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete <%=SourceTable.Name%> ");
strSql.Append(" where uid=@uid");
SqlParameter[] parameters = {
new SqlParameter("@uid", SqlDbType.Int)
};
parameters[0].Value = uid;
DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
}


/// <summary>
/// Get a Model object
/// </summary>
public NNERP.Model.<%=SourceTable.Name%> GetModel(string uid)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select * from <%=SourceTable.Name%> ");
strSql.Append(" where uid=@uid");
SqlParameter[] parameters = {
new SqlParameter("@uid", SqlDbType.Int)};
parameters[0].Value = uid;
NNERP.Model.<%=SourceTable.Name%> model=new NNERP.Model.<%=SourceTable.Name%>();
DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
model.uid=Convert.ToDecimal(uid);
if(ds.Tables[0].Rows.Count>0)
{
<%=GetModelStr()%>
}
return model;
}

/// <summary>
/// Get the data list
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select <%=GetAllColStr("")%>");
strSql.Append(" FROM <%=SourceTable.Name%> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
return DbHelperSQL.Query(strSql.ToString());
}

/// <summary>
/// get the data list by paging
/// </summary>
public DataSet GetList(int PageSize,int PageIndex,string strWhere)
{
SqlParameter[] parameters = {
new SqlParameter("@tblName", SqlDbType.VarChar, 255),
new SqlParameter("@fldName", SqlDbType.VarChar, 255),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int),
new SqlParameter("@IsReCount", SqlDbType.Bit),
new SqlParameter("@OrderType", SqlDbType.Bit),
new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
};
parameters[0].Value = "<%=SourceTable.Name%>";
parameters[1].Value = "uid";
parameters[2].Value = PageSize;
parameters[3].Value = PageIndex;
parameters[4].Value = 0;
parameters[5].Value = 0;
parameters[6].Value = strWhere;
return DbHelperSQL.RunProcedure("Proc_GetRecordByPage", parameters, "ds");
}

#endregion
}
}

你可能感兴趣的:(code)