首页 > 代码库 > 分享一套简单的CodeSmith三层模板

分享一套简单的CodeSmith三层模板

Model:

<%@ Template Language="C#" TargetLanguage="C#" %><%@ Assembly Name="SchemaExplorer" %><%@ Import Namespace="SchemaExplorer" %><%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %><%@ Property Name="NameSpace" Type="String" Description="命名空间" %>using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;namespace <%=NameSpace %>{    public partial class <%=SourceTable.Name %>    {        #region 属性        <%for(int i=0;i<SourceTable.Columns.Count;i++){ %>            /// <summary>            /// <%=SourceTable.Columns[i].Description %>            /// </summary>            public <%=SourceTable.Columns[i].SystemType %> <%=SourceTable.Columns[i].Name %> {get;set;}        <%} %>        #endregion        public <%=SourceTable.Name %>() { }        public <%=SourceTable.Name %>(DataRow dr)        {        <%for(int i=0;i<SourceTable.Columns.Count;i++){ %>            if(dr["<%=SourceTable.Columns[i].Name %>"]!=DBNull.Value)            {                this.<%=SourceTable.Columns[i].Name %>= (<%=SourceTable.Columns[i].SystemType %>)dr["<%=SourceTable.Columns[i].Name %>"];            }        <%} %>        }    }}
View Code

SqlHelper:

<%@ Template Language="C#" TargetLanguage="C#" %><%@ Property Name="NameSpace" Type="String" Category="命名空间" %>using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;using System.Data.SqlClient;using System.Data;namespace <%=NameSpace %>{    static class SqlHelper    {        public static readonly string connstr =             ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;        public static int ExecuteNonQuery(string sql,             params SqlParameter[] parameters)        {            using (SqlConnection conn = new SqlConnection(connstr))            {                conn.Open();                using (SqlCommand cmd = conn.CreateCommand())                {                    cmd.CommandText = sql;                    cmd.Parameters.AddRange(parameters);                    return cmd.ExecuteNonQuery();                }            }        }        public static object ExecuteScalar(string sql,            params SqlParameter[] parameters)        {            using (SqlConnection conn = new SqlConnection(connstr))            {                conn.Open();                using (SqlCommand cmd = conn.CreateCommand())                {                    cmd.CommandText = sql;                    cmd.Parameters.AddRange(parameters);                    return cmd.ExecuteScalar();                }            }        }        public static DataTable ExecuteDataTable(string sql,            params SqlParameter[] parameters)        {            using (SqlConnection conn = new SqlConnection(connstr))            {                conn.Open();                using (SqlCommand cmd = conn.CreateCommand())                {                    cmd.CommandText = sql;                    cmd.Parameters.AddRange(parameters);                    DataSet dataset = new DataSet();                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);                    adapter.Fill(dataset);                    return dataset.Tables[0];                }            }        }        public static object FromDbValue(object value)        {            if (value =http://www.mamicode.com/= DBNull.Value)            {                return null;            }            else            {                return value;            }        }        public static object ToDbValue(object value)        {            if (value =http://www.mamicode.com/= null)            {                return DBNull.Value;            }            else            {                return value;            }        }    }}
View Code

DAL:

<%@ Template Language="C#" TargetLanguage="C#" %><%@ Assembly Name="SchemaExplorer" %><%@ Import Namespace="SchemaExplorer" %><%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="数据库" %><%@ Property Name="NameSpace" Type="String" Description="命名空间" %>using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace <%=NameSpace %>{    public static partial class <%=SourceTable.Name %>DAL    {        public static List<<%=SourceTable.Name %>> Search(string sqlStr,List<SqlParameter> pms)        {            List<<%=SourceTable.Name %>> list = new List<<%=SourceTable.Name %>>();            DataTable table = SqlHelper.ExecuteDataTable(sqlStr,pms.ToArray());            foreach (DataRow dr in table.Rows)            {                <%=SourceTable.Name %> model = new <%=SourceTable.Name %>(dr);                list.Add(model);            }            return list;        }        public static bool Insert(<%=SourceTable.Name %> model)        {            string sqlStr = "";            List<string> fileds = new List<string>();            List<string> pFileds = new List<string>();            List<SqlParameter> pms = new List<SqlParameter>();            #region 添加字段            <%for(int i=0;i<SourceTable.Columns.Count;i++){ %>                        <%if((bool)(SourceTable.Columns[i].ExtendedProperties["CS_IsIdentity"].Value)==true){continue;} %>                            <%if(SourceTable.Columns[i].SystemType==typeof(DateTime))                { %>                if(model.<%=SourceTable.Columns[i].Name %>!=null&&model.<%=SourceTable.Columns[i].Name %>!=new DateTime())                {                    fileds.Add("[<%=SourceTable.Columns[i].Name %>]");                    pFileds.Add("@<%=SourceTable.Columns[i].Name %>");                    pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));                }                <% }else {%>                 if(model.<%=SourceTable.Columns[i].Name %>!=null)                {                    fileds.Add("[<%=SourceTable.Columns[i].Name %>]");                    pFileds.Add("@<%=SourceTable.Columns[i].Name %>");                    pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));                }                <%} %>                            <%} %>            #endregion            StringBuilder sb = new StringBuilder();            sb.Append("INSERT INTO <%=SourceTable.Name %> (");            sb.Append(string.Join(",", fileds));            sb.Append(") values (");            sb.Append(string.Join(",", pFileds));            sb.Append(")");            sqlStr = sb.ToString();            int i= SqlHelper.ExecuteNonQuery(sqlStr, pms.ToArray());            return i>0;        }                public static bool Update(<%=SourceTable.Name %> model)        {            string sqlStr = "";            List<string> fileds = new List<string>();            List<string> pFileds = new List<string>();            List<SqlParameter> pms = new List<SqlParameter>();            #region 添加字段            <%for(int i=0;i<SourceTable.Columns.Count;i++){ %>                        <%if(SourceTable.Columns[i].IsPrimaryKeyMember){ %>            pFileds.Add("[<%=SourceTable.Columns[i].Name %>]=@<%=SourceTable.Columns[i].Name %>");            pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));            <%} else{ %>                            <%if(SourceTable.Columns[i].SystemType==typeof(DateTime))                { %>                if(model.<%=SourceTable.Columns[i].Name %>!=null&&model.<%=SourceTable.Columns[i].Name %>!=new DateTime())                {                    fileds.Add("[<%=SourceTable.Columns[i].Name %>]=@<%=SourceTable.Columns[i].Name %>");                    pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));                }                <% }else {%>                 if(model.<%=SourceTable.Columns[i].Name %>!=null)                {                    fileds.Add("[<%=SourceTable.Columns[i].Name %>]=@<%=SourceTable.Columns[i].Name %>");                    pms.Add(new SqlParameter("<%=SourceTable.Columns[i].Name %>", model.<%=SourceTable.Columns[i].Name %>));                }                <%} %>                <%} %>            <%} %>            #endregion            StringBuilder sb = new StringBuilder();            sb.Append("update <%=SourceTable.Name %> set ");            sb.Append(string.Join(",", fileds));            sb.Append(" where ");            sb.Append(string.Join(" and ", pFileds));            sqlStr = sb.ToString();            int i= SqlHelper.ExecuteNonQuery(sqlStr, pms.ToArray());            return i>0;        }    }}
View Code

Tables:遍历库中所有表

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="List all database tables" %><%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Database containing the tables." %><%@ Assembly Name="SchemaExplorer" %><%@ Import Namespace="SchemaExplorer" %>Tables in database "<%= SourceDatabase %>":<% for (int i = 0; i < SourceDatabase.Tables.Count; i++) { %>        <%= SourceDatabase.Tables[i].Name %><% } %>
View Code