• codesmith 数据层 模板 让操作更简单


    <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
    <%@ Assembly Name="SchemaExplorer" %>

    <%@ Assembly Name="System.Data" %>

    <%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context"      Description="TargetTable that the object is based on." %>

    <%@ Property Name="Author" Type="System.String" Default="Xiao ChangJian"  Category="Context"  Description="The author for this procedure." %>

    <%@ Property Name="NameSpace" Type="System.String" Default="MySpace"  Category="Context"  Description="TargetTable that the object is based on." %>

    <%@ Property Name="Description" Type="System.String" Default="" Category="Context" Description="The description for this procedure."%>

    <%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>

    <%@ Import Namespace="SchemaExplorer" %>

    <%@ Import Namespace="System.Data" %>

    /* ----------------------------------------------------------------------

    * File Name: <%= GetTableName(TargetTable) %>Dal.cs

    *

    * Create Author: <%= Author %>

    *

    * Create DateTime: <%=DateTime.Now.ToShortDateString() %>

    *

    * Description: <%= Description %>

    *----------------------------------------------------------------------*/

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Data;

    using System.Data.SqlClient;

    using DBUtilitys;

    namespace <%= NameSpace %>

    {

    public class <%= GetTableName(TargetTable) %>Dal

    {

    private static readonly <%= GetTableName(TargetTable) %>Dal _classinstance = new <%= GetTableName(TargetTable) %>Dal();

    public static <%= GetTableName(TargetTable) %>Dal ClassInstance {get{return _classinstance; }}

    /// <summary>

    /// Add Record

    /// </summary>

    public int Insert_<%= GetTableName(TargetTable) %>(<%= GetTableName(TargetTable) %>Model instance)

    {

    string executesqlstr =  "INSERT INTO <%= TargetTable.Name %> (<%= GetColumnNameNoPK(TargetTable) %> ) VALUES (<%= EvaluateColumnValueNoPK(TargetTable) %>)";

    SqlParameter[] para = new SqlParameter[] {

    <% int i = 0; foreach(ColumnSchema column in TargetTable.NonPrimaryKeyColumns){ %>

    new SqlParameter("@<%= column.Name %>",instance.<%= column.Name %>)<% if(i < TargetTable.Columns.Count-2){%>,<%} %>

    <% i++; } %>

    };

    return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, executesqlstr, para);

    }

    /// <summary>

    /// Delete Record

    /// </summary>

    public int Delete_<%= GetTableName(TargetTable) %>(String pkid)

    {

    string executesqlstr = "DELETE <%= TargetTable.Name %> WHERE <%= GetPrimaryKeyName(TargetTable) %> = @<%= GetPrimaryKeyName(TargetTable) %>";

    SqlParameter[] para = new SqlParameter[] {

    new SqlParameter("@<%= GetPrimaryKeyName(TargetTable) %>",pkid)

    };

    return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, executesqlstr, para);

    }

    /// <summary>

    /// Update Record

    /// </summary>

    public int Update_<%= GetTableName(TargetTable) %>(<%= GetTableName(TargetTable) %>Model instance)

    {

    string executesqlstr = "UPDATE <%= TargetTable.Name %> SET <%= UpdateColumn(TargetTable) %> WHERE <%= GetPrimaryKeyName(TargetTable) %> = @<%= GetPrimaryKeyName(TargetTable) %>";

    SqlParameter[] para = new SqlParameter[] {

    <% int j = 0; foreach(ColumnSchema column in TargetTable.Columns){ %>

    new SqlParameter("@<%= column.Name %>",instance.<%= column.Name %>)<% if(j < TargetTable.Columns.Count-1){%>,<%} %>

    <% j++; } %>

    };

    return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, executesqlstr, para);

    }

    /// <summary>

    /// Get Record By Primary Key

    /// </summary>

    public <%= GetTableName(TargetTable) %>Model Get_<%= GetTableName(TargetTable) %>ByPKID(String pkid)

    {

    string executesqlstr = "SELECT <%= GetColumnName(TargetTable) %> FROM <%= GetTableName(TargetTable) %> WHERE <%= GetPrimaryKeyName(TargetTable) %> = @<%= GetPrimaryKeyName(TargetTable) %>";

    SqlParameter[] para = new SqlParameter[] {

    new SqlParameter("@<%= GetPrimaryKeyName(TargetTable) %>",pkid)

    };

    <%= GetTableName(TargetTable) %>Model modelinstance = new <%= GetTableName(TargetTable) %>Model();

    using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString, CommandType.Text, executesqlstr, para))

    {

    if (sdr.Read())

    {

    modelinstance = EvaluateModel(sdr);

    }

    }

    return modelinstance;

    }


    作者:水木    
     
  • 相关阅读:
    python | 微信轰炸脚本代码
    周末学习记录(摘抄为主)
    前端便捷开发测试示例
    前端大图预加载代码段
    一个蛋疼的数组操作问题
    好好回顾javascript基础知识
    jquery学习笔记
    很短的一个针对IE的感想
    放大镜查看大图代码
    判断ie浏览器7、8、9三个版本
  • 原文地址:https://www.cnblogs.com/hsapphire/p/1649931.html
Copyright © 2020-2023  润新知