使用CodeSmith生成DAL操作模版

<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema" Default="" Optional="false" Category="" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="NameSpace" Type="System.String" Default="Szjdw.DAL" Optional="false" Category="" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="opuser" Type="System.String" Default="" Optional="False" Category="" Description="请输入您的名字" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
/*
*本代码由代码生成器自动生成,请不要更改此文件的任何代码,如需要添加方法,请新建文件,并使用partial修饰符添加附加方法。
*生成时间:<%= DateTime.Now.ToLocalTime() %>
*生成者:<%= opuser %>
*/
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Szjdw;
using Szjdw.Model;
using Szjdw.Data.Provider;
using Szjdw.Down;

namespace Szjdw.DAL
{
    ///<summary>
    ///表<%=TableName.Name%>的实体类
    ///</summary>
    public class <%=TableName.Name%>View
    {
        /// <summary>
        /// 将数据插入表
        /// </summary>
        /// <param name="M">赋值后的实体</param>
        /// <returns></returns>
        public static int Insert(<%=TableName.Name%> M)
        {
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            <%for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++) {%>
            Sql.Add("@<%=GetSqlParameterStatement(TableName.NonPrimaryKeyColumns[i])%>",<%=GetSqlDBType(TableName.Columns[i].NativeType)%>M.<%= StringUtil.ToPascalCase(TableName.Columns[i].Name) %>,<%= TableName.Columns[i].Size%>);
            <%}%>
            return Convert.ToInt32(Sql.ExecuteScalar("<%=TableName.Name%>_Insert").ToString());
        }
       
        /// <summary>
        /// 将修改过的实体修改到数据库
        /// </summary>
        /// <param name="M">赋值后的实体</param>
        /// <returns></returns>
        public static void Update(<%=TableName.Name%> M)
        {
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            <%for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++) {%>
            Sql.Add("@<%=GetSqlParameterStatement(TableName.NonPrimaryKeyColumns[i])%>",<%=GetSqlDBType(TableName.Columns[i].NativeType)%>M.<%= StringUtil.ToPascalCase(CSharpAlias[TableName.Columns[i].Name]) %>,<%= TableName.Columns[i].Size%>);
            <%}%>
            Sql.ExecuteNonQuery("<%=TableName.Name%>_Update");
        }
       
        /// <summary>
        /// 根据ID取得实体
        /// </summary>
        /// <param name="id">id,即编号主键</param>
        /// <returns></returns>
        public static <%=TableName.Name%> GetModelByID(int id)
        {
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            <%=TableName.Name%> M = new <%=TableName.Name%>();
            SqlDataReader Rs = Sql.ExecuteReader(CommandType.Text, "select <%= getColunms() %> from <%=TableName.Name%> where id=" + id.ToString(), SqlHelper.ConnClose.Yes);
            if (!Rs.Read())
            {
                M.Id=int.MinValue;
                return M;
            }
            <% for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++){ %>
            M.<%= StringUtil.ToPascalCase(TableName.Columns[i].Name) %>=Convert.<%=GetCSharpVariableType(TableName.Columns[i])%>(Rs["<%= TableName.Columns[i].Name %>"]);
            <%}%>
            Rs.Close();
            Rs = null;
            return M;
        }
       
        /// <summary>
        /// 根据条件语句取得实体
        /// </summary>
        /// <param name="m_where">条件语句,不包含“where”</param>
        /// <returns></returns>
        public static <%=TableName.Name%> Find(string m_where)
        {
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            <%=TableName.Name%> M = new <%=TableName.Name%>();
            SqlDataReader Rs = Sql.ExecuteReader(CommandType.Text, "select <%= getColunms() %> from Ws_Software where " + m_where, SqlHelper.ConnClose.Yes);
            if (!Rs.Read())
            {
                M.Id=int.MinValue;
                return M;
            }
            <% for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++){ %>
            M.<%= StringUtil.ToPascalCase(TableName.Columns[i].Name) %>=Convert.<%=GetCSharpVariableType(TableName.Columns[i])%>(Rs["<%= TableName.Columns[i].Name %>"]);
            <%}%>
            Rs.Close();
            Rs = null;
            return M;
        }
        /// <summary>
        /// 根据条件语句取得符合条件的数据表
        /// </summary>
        /// <param name="m_where">条件语句,不包含“where”</param>
        /// <returns></returns>
        public static DataTable getTable(string m_where)
        {
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            return Sql.ExecuteDataTable(CommandType.Text, "select <%= getColunms() %> from <%= TableName.Name %> where "+ m_where);
        }
       
        /// <summary>
        /// 获取符合条件记录的条数
        /// </summary>
        /// <param name="m_where">条件语句,不包含“where”</param>
        /// <returns></returns>
        public static int Count(string m_where)
        {
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            return Convert.ToInt32(Sql.ExecuteScalar(CommandType.Text,"select count(1) from <%=TableName.Name%>"));
        }
       
        /// <summary>
        /// 验证符合条件的记录是否存在
        /// </summary>
        /// <param name="m_where">条件语句,不包含“where”</param>
        /// <returns></returns>
        public static bool Exist(string m_where)
        {
            if(Count(m_where)>0)
            {
                return true;
            }
            return false;
        }
       
        /// <summary>
        /// 获取符合条件记录的条数
        /// </summary>
        /// <param name="m_where">条件语句,不包含“where”</param>
        /// <returns></returns>
        public static List<<%=TableName.Name%>> GetModelList(string m_where)
        {   
            List<<%=TableName.Name%>> Ms=new List<<%=TableName.Name%>>();
            SqlHelper Sql = new SqlHelper(SqlExt.ConnStr);
            DataTable dt=Sql.ExecuteDataTable(CommandType.Text, "select <%= getColunms() %> from <%= TableName.Name %> where "+m_where);
            for(int i=0;i<dt.Rows.Count;i++)
            {
                <%=TableName.Name%> M=new <%=TableName.Name%>();
                <% for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++){ %>
                M.<%= StringUtil.ToPascalCase(TableName.Columns[i].Name) %>=Convert.<%=GetCSharpVariableType(TableName.Columns[i])%>(dt.Rows[i]["<%= TableName.Columns[i].Name %>"].ToString());
                <%}%>
                Ms.Add(M);
            }
            return Ms;
        }
       
    }
   
   
}

<script runat="template">
// My methods here.

public string getColunms()
{
    string returnValue="";
    for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++)
    {
        returnValue+=TableName.Columns[i].Name+",";
    }
    returnValue=returnValue.TrimEnd(',');
    return returnValue;
}

public string GetSqlDBType(string type)
{
    switch(type)
    {
        case "int": return "SqlDbType.Int,";
        case "tinyint": return "SqlDbType.TinyInt,";
        case "nvarchar": return "SqlDbType.NVarChar,";
        case "datetime": return "SqlDbType.DateTime,";
        case "char": return "SqlDbType.Char,";
        case "decimal": return "SqlDbType.Decimal,";
        case "float": return "SqlDbType.Float,";
        case "image": return "SqlDbType.Image,";
        case "money": return "SqlDbType.Money,";
        case "nchar": return "SqlDbType.NChar,";
        case "ntext": return "SqlDbType.NText,";
        case "real": return "SqlDbType.Real,";
        case "smalldatetime": return "SqlDbType.SmallDateTime,";
        case "smallint": return "SqlDbType.SmallInt,";
        case "smallmoney": return "SqlDbType.SmallMoney,";
        case "text": return "SqlDbType.Text,";
        case "timestamp": return "SqlDbType.Timestamp,";
        case "udt": return "SqlDbType.Udt,";
        case "uniqueidentifier": return "SqlDbType.UniqueIdentifier,";
        case "varbinary": return "SqlDbType.VarBinary,";
        case "varchar": return "SqlDbType.VarChar,";
        case "variant": return "SqlDbType.Variant,";
        case "xml": return "SqlDbType.Xml,";
        default : return "";
    }
   
}
public string GetSqlParameterStatement(ColumnSchema column)
{
    return column.Name;
}
public string GetCSharpVariableType(ColumnSchema column)
{
    if (column.Name.EndsWith("TypeCode")) return column.Name;
   
    switch (column.DataType)
    {
        case DbType.AnsiString: return "ToString";
        case DbType.AnsiStringFixedLength: return "ToString";
        case DbType.Binary: return "byte[]";
        case DbType.Boolean: return "ToBoolean";
        case DbType.Byte: return "ToByte";
        case DbType.Currency: return "ToDecimal";
        case DbType.Date: return "ToDateTime";
        case DbType.DateTime: return "ToDateTime";
        case DbType.Decimal: return "ToDecimal";
        case DbType.Double: return "ToDouble";
        case DbType.Guid: return "Guid";
        case DbType.Int16: return "ToInt16";
        case DbType.Int32: return "ToInt32";
        case DbType.Int64: return "ToInt64";
        case DbType.Object: return "object";
        case DbType.SByte: return "ToSByte";
        case DbType.Single: return "ToDecimal";
        case DbType.String: return "ToString";
        case DbType.StringFixedLength: return "ToString";
        case DbType.Time: return "ToDateTime";
        case DbType.UInt16: return "ToUInt16";
        case DbType.UInt32: return "ToUInt32";
        case DbType.UInt64: return "ToUInt64";
        case DbType.VarNumeric: return "ToDecimal";
        default:
        {
            return "__UNKNOWN__" + column.NativeType;
        }
    }
}
</script>

你可能感兴趣的:(code)