最基本的DAL的CodeSmith摸板

<%--
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="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" %>
/*
*本代码由代码生成器自动生成,请不要更改此文件的任何代码,如需要添加方法,请新建文件,并使用partial修饰符添加附加方法。
*生成时间:<%= DateTime.Now.ToLocalTime() %>
*生成者:<%= opuser %>
*/
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Model;


namespace <%=NameSpace%>
{
    ///<summary>
    ///表<%=TableName.Name%>的实体类
    ///</summary>
    public class <%=TableName.Name%>
    {
           
        #region 成员方法
       
        /// <summary>
        /// 将数据插入表
        /// </summary>
        /// <returns></returns>
        public bool Exists(<% for (int i = 0; i < TableName.PrimaryKey.MemberColumns.Count; i++) { %><%=DataType2CSharpType(TableName.PrimaryKey.MemberColumns[i].DataType)%> <%=TableName.PrimaryKey.MemberColumns[i].Name%><% if (i < TableName.PrimaryKey.MemberColumns.Count-1) { %>, <% } %><% } %>)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) from <%=TableName%> where ");
            <% for (int i = 0; i < TableName.PrimaryKey.MemberColumns.Count; i++) { %>
            strSql.Append("<%=TableName.PrimaryKey.MemberColumns[i].Name%>="+<%=TableName.PrimaryKey.MemberColumns[i].Name%><%if(i<TableName.PrimaryKey.MemberColumns.Count-1){%>,<%}else{%>+")"<%}%>);
            <%}%>
            return DbHelperSQL.Exists(strSql.ToString());
        }
       
        /// <summary>
        /// 将数据插入表
        /// </summary>
        /// <returns></returns>
        public int Insert()
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into <%=TableName%>(");
            <%for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++) {%>
            strSql.Append(<%=TableName.NonPrimaryKeyColumns[i].Name%>);
            <%if(i<TableName.NonPrimaryKeyColumns.Count-1){%>
            strSql.Append(",");
            <%}else{%>
            strSql(")");
            <%}%>
            <% } %>
            strSql.Append(";select @@identity");
            Object obj=DbHelperSQL.GetSingle(strSql.ToString());
            if(obj==null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
       
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <returns></returns>
        public void Update()
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("update <%=TableName%> set ");
            <%for(int i=0;i<TableName.NonPrimaryKeyColumns.Count;i++) {%>
            strSql.Append("<%=TableName.NonPrimaryKeyColumns[i].Name%>="+<%=TableName.NonPrimaryKeyColumns[i].Name%><%if(i<TableName.NonPrimaryKeyColumns.Count-1){%>+","<%}else{%>+")"<%}%>);
            <%}%>
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
             
        #endregion 成员方法
}

<script runat="template">
    public string DataType2CSharpType(System.Data.DbType dbType)
    {
        switch (dbType)
        {
            case DbType.AnsiString:
                return "string";
            case DbType.AnsiStringFixedLength:
                return "string";
            case DbType.Binary:
                return "byte[]";
            case DbType.Boolean:
                return "bool";
            case DbType.Byte:
                return "byte";
            case DbType.Currency:
                return "decimal";
            case DbType.Date:
                return "DateTime";
            case DbType.DateTime:
                return "DateTime";
            case DbType.DateTime2:
                return "DateTime";
            case DbType.DateTimeOffset:
                return "DateTime";
            case DbType.Decimal:
                return "decimal";
            case DbType.Double:
                return "double";
            case DbType.Guid:
                return "Guid";
            case DbType.Int16:
                return "short";
            case DbType.Int32:
                return "int";
            case DbType.Int64:
                return "long";
            case DbType.Object:
                return "object";
            case DbType.SByte:
                return "sbyte";
            case DbType.Single:
                return "float";
            case DbType.String:
                return "string";
            case DbType.StringFixedLength:
                return "string";
            case DbType.Time:
                return "DateTime";             
            case DbType.UInt16:
                return "ushort";
            case DbType.UInt32:
                return "uint";
            case DbType.UInt64:
                return "ulong";
            case DbType.VarNumeric:
                return "decimal";
            case DbType.Xml:
                return "string";
            default:
                return "object";
        }
    }
   
    public string GetSqlParameterStatement(ColumnSchema column)
    {
        string param = "@" + column.Name + " " + column.NativeType;
        switch (column.DataType)
        {
            case DbType.Decimal:
            {
                param += "(" + column.Precision + ", " + column.Scale + ")";
                break;
            }
            default:
            {
                if (column.Size > 0)
                {
                    param += "(" + column.Size + ")";
                }
                break;
            }
        }
        return param;
    }
   
</script>

 

你可能感兴趣的:(code)