码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-20 10:08:00      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

Model:

bubuko.com,布布扣
<%@ 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:

bubuko.com,布布扣
<%@ 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 == DBNull.Value)
            {
                return null;
            }
            else
            {
                return value;
            }
        }

        public static object ToDbValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }
    }
}
View Code

DAL:

bubuko.com,布布扣
<%@ 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:遍历库中所有表

bubuko.com,布布扣
<%@ 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

分享一套简单的CodeSmith三层模板,布布扣,bubuko.com

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

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/uptothesky/p/3855563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!