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;

}

 

 

你可能感兴趣的:(code)