`
lovnet
  • 浏览: 6820735 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

CodeSmith生成三层代码之数据访问层(3)

SQL 
阅读更多

<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="请选择数据表!" %>
<%@ Property Name="NameSpace" Type="System.String" Default="" Optional="False" Category="Description" Description="请输入命名空间" %>
<%@ Property Name="DefaultDatabase" Type="System.Boolean" Default="True" Optional="False" Category="Description" Description="请输入命名空间" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
/*************************************************************/
/* CodeSmith模板生成数据访问 */
/*************************************************************/
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
using <%=NameSpace%>.DBUtility;

namespace <%=NameSpace%>.SQLServerDAL
{
/// <summary>
/// 数据访问类<%=SourceTable.Name%>
/// </summary>
public class <%=SourceTable.Name%> : <%=NameSpace%>.IDAL.I<%=SourceTable.Name%>
{
<% if(!DefaultDatabase) {%>DbHelperSQLP DbHelperSQL = new DbHelperSQL();<%}%>
#region CodeSmith自动生成
/// <summary>
/// 得到一个DataSet
/// </summary>
public DataSet GetData(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select <%=GetColumnNameList(SourceTable)%> ");
strSql.Append(" FROM <%=SourceTable.Name%> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
return DbHelperSQL.Query(strSql.ToString());
}

/// <summary>
/// 增加一条数据
/// </summary>
public void Add(<%=NameSpace%>.Model.<%=SourceTable.Name%> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into <%=SourceTable.Name%>(");
strSql.Append("<%=GetColumnNameList(SourceTable)%>)");
strSql.Append(" values (");
strSql.Append("<%=GetColumnNameListPara(SourceTable)%>)");
SqlParameter[] parameters = {
<%for(int i=0; i<SourceTable.Columns.Count; i++) {%>
<%if(i == SourceTable.Columns.Count -1){%>new SqlParameter("@<%=SourceTable.Columns[i].Name%>", <%=GetSqlDbType(SourceTable.Columns[i])%>,<%=SourceTable.Columns[i].Size%>)<%}else{%>new SqlParameter("@<%=SourceTable.Columns[i].Name%>", <%=GetSqlDbType(SourceTable.Columns[i])%>,<%=SourceTable.Columns[i].Size%>),<%}%>
<%}%>
};
<%for(int i=0; i<SourceTable.Columns.Count; i++) {%>
parameters[<%=i%>].Value = model.<%=SourceTable.Columns[i].Name%>;
<%}%>

DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
}
/// <summary>
/// 更新一条数据
/// </summary>
public void Update(<%=NameSpace%>.Model.<%=SourceTable.Name%> model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update <%=SourceTable.Name%> set ");
strSql.Append("<%=GetUpdateColumn(SourceTable)%>");
strSql.Append(" where <%=GetKeyColumn(SourceTable).Name%>=@<%=GetKeyColumn(SourceTable).Name%> ");
SqlParameter[] parameters = {
<%for(int i=0; i<SourceTable.Columns.Count; i++) {%>
<%if(i == SourceTable.Columns.Count -1){%>new SqlParameter("@<%=SourceTable.Columns[i].Name%>", <%=GetSqlDbType(SourceTable.Columns[i])%>,<%=SourceTable.Columns[i].Size%>)<%}else{%>new SqlParameter("@<%=SourceTable.Columns[i].Name%>", <%=GetSqlDbType(SourceTable.Columns[i])%>,<%=SourceTable.Columns[i].Size%>),<%}%>
<%}%>
};
<%for(int i=0; i<SourceTable.Columns.Count; i++) {%>
parameters[<%=i%>].Value = model.<%=SourceTable.Columns[i].Name%>;
<%}%>

DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
}

/// <summary>
/// 删除数据
/// </summary>
public void Delete(<% =GetPrimaryKeyType(SourceTable) + " " + GetKeyColumn(SourceTable).Name.ToLower()%>)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("delete <% =SourceTable.Name%> ");
strSql.Append(" where <% =GetKeyColumn(SourceTable).Name %>=@<% =GetKeyColumn(SourceTable).Name %> ");
SqlParameter[] parameters = {
new SqlParameter("@<% =GetKeyColumn(SourceTable).Name %>", <%=GetKeyColumnSqlType(SourceTable)%>, <%=GetKeyColumnSqlLength(SourceTable)%>)};
parameters[0].Value = <% =GetKeyColumn(SourceTable).Name.ToLower() %>;

DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
}

/// <summary>
/// 得到一个泛型集合
/// </summary>
public List<<%=NameSpace%>.Model.<%=SourceTable.Name%>> GetList(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select <%=GetColumnNameList(SourceTable)%> ");
strSql.Append(" FROM <%=SourceTable.Name%> ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}

List<<%=NameSpace%>.Model.<%=SourceTable.Name%>> list = new List<<%=NameSpace%>.Model.<%=SourceTable.Name%>>();

using (SqlDataReader reader = DbHelperSQL.ExecuteReader(strSql.ToString()))
{
while (reader.Read())
{
<%=NameSpace%>.Model.<%=SourceTable.Name%> model = new <%=NameSpace%>.Model.<%=SourceTable.Name%>();
<% for(int i=0; i<SourceTable.Columns.Count; i++){%>
model.<%=SourceTable.Columns[i].Name%> = <%=GetSqlReader(SourceTable.Columns[i])%>
<%}%>
list.Add(model);
}
reader.Close();
}

return list;
}

/// <summary>
/// 得到一个对象实体
/// </summary>
public <%=NameSpace%>.Model.<%=SourceTable.Name%> GetModel(<% =GetPrimaryKeyType(SourceTable) + " " + GetKeyColumn(SourceTable).Name.ToLower()%>)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select <%=GetColumnNameList(SourceTable)%> from <%=SourceTable.Name%> ");
strSql.Append(" where <%=GetKeyColumn(SourceTable).Name%>=@<%=GetKeyColumn(SourceTable).Name%> ");
SqlParameter[] parameters = {
new SqlParameter("@<% =GetKeyColumn(SourceTable).Name %>", <%=GetKeyColumnSqlType(SourceTable)%>, <%=GetKeyColumnSqlLength(SourceTable)%>)};
parameters[0].Value = <%=GetKeyColumn(SourceTable).Name.ToLower()%>;

<%=NameSpace%>.Model.<%=SourceTable.Name%> model = new <%=NameSpace%>.Model.<%=SourceTable.Name%>();
DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
if(ds.Tables[0].Rows.Count>0)
{
<% for(int i=0; i<SourceTable.Columns.Count; i++){%>
model.<%=SourceTable.Columns[i].Name%> = <%=GetDataSet(SourceTable.Columns[i])%>
<%}%>
return model;
}
else
{
return null;
}
}
#endregion
}
}
<script runat="template">
public string GetUpdateColumn(TableSchema table)
{
string strUpdate = "";
for(int i=0; i<table.Columns.Count; i++)
{
if(!table.Columns[i].IsPrimaryKeyMember)
{
if(strUpdate == string.Empty)
strUpdate = "[" + table.Columns[i].Name + "]=@" + table.Columns[i].Name;
else
strUpdate = strUpdate + "," + "[" + table.Columns[i].Name + "]=@" + table.Columns[i].Name;
}
}
return strUpdate;
}

public string GetDataSet(ColumnSchema column)
{
string sqlReader = "ds.Tables[0].Rows[0][\"" + column.Name + "\"]";

string csharpType = GetCSharpType(column);
if(csharpType.ToLower() == "string")
return sqlReader + ".ToString()";

string temp = "(" + GetCSharpType(column) + ")" + sqlReader;
if(column.AllowDBNull)
{
temp = sqlReader + ".ToString() == Strimg.Empty ? null : " + temp;
}

temp = temp + ";";
return temp;
}

public string GetSqlReader(ColumnSchema column)
{
string sqlReader = "reader[\"" + column.Name + "\"]";

string csharpType = GetCSharpType(column);
if(csharpType.ToLower() == "string")
return sqlReader + ".ToString()";

string temp = "(" + GetCSharpType(column) + ")" + sqlReader;
if(column.AllowDBNull)
{
temp = sqlReader + ".ToString() == Strimg.Empty ? null : " + temp;
}

temp = temp + ";";
return temp;
}

public string GetColumnNameListPara(TableSchema table)
{
string columnList = "";

for(int i=0; i<table.Columns.Count; i++)
{
if(columnList == string.Empty)
columnList = "@" + table.Columns[i].Name;
else
columnList = columnList + ",@" + table.Columns[i].Name;
}

return columnList;
}

public string GetColumnNameList(TableSchema table)
{
string columnList = "";

for(int i=0; i<table.Columns.Count; i++)
{
if(columnList == string.Empty)
columnList = "[" + table.Columns[i].Name + "]";
else
columnList = columnList + ",[" + table.Columns[i].Name + "]";
}

return columnList;
}

public ColumnSchema GetKeyColumn(TableSchema table)
{
ColumnSchema column = null;
for(int i=0; i<table.Columns.Count; i++)
{
if(table.Columns[i].IsPrimaryKeyMember)
column = table.Columns[i];
}
return column;
}

public string GetKeyColumnSqlType(TableSchema table)
{
ColumnSchema column = GetKeyColumn(table);
if(column == null)
return string.Empty;

return GetSqlDbType(column);
}

public int GetKeyColumnSqlLength(TableSchema table)
{
ColumnSchema column = GetKeyColumn(table);
if(column == null)
return 0;

return column.Size;
}

public string GetPrimaryKeyType(TableSchema table)
{
int columnIndex = 0;
for(int i=0; i<table.Columns.Count; i++)
{
if(table.Columns[i].IsPrimaryKeyMember)
columnIndex = i;
}

return GetCSharpType(table.Columns[columnIndex]);
}

public string GetSqlDbType(ColumnSchema column)
{
switch (column.NativeType.ToLower())
{
case "bigint": return "SqlDbType.BigInt";
case "binary": return "SqlDbType.Binary";
case "bit": return "SqlDbType.Bit";
case "char": return "SqlDbType.Char";
case "datetime": return "SqlDbType.DateTime";
case "decimal": return "SqlDbType.Decimal";
case "float": return "SqlDbType.Float";
case "image": return "SqlDbType.Image";
case "int": return "SqlDbType.Int";
case "money": return "SqlDbType.Money";
case "nchar": return "SqlDbType.NChar";
case "ntext": return "SqlDbType.NText";
case "numeric": return "SqlDbType.Decimal";
case "nvarchar": return "SqlDbType.NVarChar";
case "real": return "SqlDbType.Real";
case "smalldatetime": return "SqlDbType.SmallDateTime";
case "smallint": return "SqlDbType.SmallInt";
case "smallmoney": return "SqlDbType.SmallMoney";
case "sql_variant": return "SqlDbType.Variant";
case "sysname": return "SqlDbType.NChar";
case "text": return "SqlDbType.Text";
case "timestamp": return "SqlDbType.Timestamp";
case "tinyint": return "SqlDbType.TinyInt";
case "uniqueidentifier": return "SqlDbType.UniqueIdentifier";
case "varbinary": return "SqlDbType.VarBinary";
case "varchar": return "SqlDbType.VarChar";
default: return "__UNKNOWN__" + column.NativeType;
}
}

public string GetCSharpType(ColumnSchema column)
{
string para = "";
if (column.Name.EndsWith("TypeCode"))
return column.Name;

switch (column.DataType)
{
case DbType.AnsiString:
para = "string";
break;
case DbType.AnsiStringFixedLength:
para = "string";
break;
case DbType.Binary:
para = "byte[]";
break;
case DbType.Boolean:
para = "bool";
break;
case DbType.Byte:
para = "int";
break;
case DbType.Currency:
para = "decimal";
break;
case DbType.Date:
para = "DateTime";
break;
case DbType.DateTime:
para = "DateTime";
break;
case DbType.Decimal:
para = "decimal";
break;
case DbType.Double:
para = "double";
break;
case DbType.Guid:
para = "Guid";
break;
case DbType.Int16:
para = "short";
break;
case DbType.Int32:
para = "int";
break;
case DbType.Int64:
para = "long";
break;
case DbType.Object:
para = "object";
break;
case DbType.SByte:
para = "sbyte";
break;
case DbType.Single:
para = "float";
break;
case DbType.String:
para = "string";
break;
case DbType.StringFixedLength:
para = "string";
break;
case DbType.Time:
para = "TimeSpan";
break;
case DbType.UInt16:
para = "ushort";
break;
case DbType.UInt32:
para = "uint";
break;
case DbType.UInt64:
para = "ulong";
break;
case DbType.VarNumeric:
para = "decimal";
break;
default:
para = "__UNKNOWN__" + column.NativeType;
break;
}

return para;
}
</script>

分享到:
评论

相关推荐

    codesmith 生成三层数据访问层

    根据提供的文件信息,我们可以深入探讨如何使用 Codesmith 来生成三层架构中的数据访问层(DAL)。三层架构是一种常见的软件架构模式,它将应用程序分为三个主要的逻辑层:表示层、业务逻辑层和数据访问层。这种分层...

    CodeSmith生成三层访问代码

    在本案例中,我们关注的是如何使用CodeSmith来生成三层架构的访问代码,这通常涉及到数据访问层(DAL)、业务逻辑层(BLL)和表现层(UI)。 三层架构是一种常见的软件设计模式,它将应用逻辑分为三个独立的层次,...

    CodeSmith 生成三层模板

    通过使用CodeSmith生成三层模板和SqlHelper,开发者可以快速搭建起一个基础的、具备数据访问能力的.NET应用程序框架,极大地减少了编码工作量,使得开发者能更专注于业务逻辑的实现和系统的优化。这不仅提高了开发...

    codeSmith自动生成三层中的代码

    在使用CodeSmith生成三层架构代码时,你需要创建相应的模板文件。模板语言是CodeSmith的强项,它允许使用C#或其他.NET语言的语法来定义模板。例如,你可以创建一个模板来生成基于Entity Framework的DAL,另一个模板...

    ASP.NET 中 CodeSmith可以自动生成三层代码

    通过以上步骤,开发者可以利用CodeSmith在ASP.NET项目中快速生成符合三层架构的代码,从而节省大量手动编码的时间,提高项目的开发速度。同时,由于模板的灵活性,即使项目需求变更,只需更新模板即可自动生成适应新...

    CodeSmith三层代码生成模板

    在CodeSmith生成的三层代码中,这一层负责与用户交互,接收用户的输入并显示结果。它不包含具体的业务逻辑,而是调用业务逻辑层来处理这些请求。文件名`DBMad.BLL.cst`可能包含了与业务逻辑层交互的接口或代理类。 ...

    普通 codesmith三层代码生成模板

    "普通 codesmith三层代码生成模板" 是一个专为C#开发者设计的工具,用于自动化生成代码,特别是在开发三层架构的应用程序时。三层架构通常包括表现层(Presentation Layer)、业务逻辑层(Business Logic Layer)和...

    CodeSmith .net 三层自动生成模板

    在.NET 2.0时代,CodeSmith就已经被广泛应用于项目开发中,尤其是对于三层架构(数据访问层、业务逻辑层、表示层)的应用,它的模板功能尤为突出。 三层架构是一种常见的软件设计模式,它将应用程序分为三个主要...

    CodeSmith软件以及三层结构代码生成模板及DBHelper类

    三层结构是软件设计中的一种常见模式,包括表现层(UI)、业务逻辑层(BLL)和数据访问层(DAL)。这种结构有助于保持代码的清晰和模块化,提高代码的可维护性和可重用性。使用CodeSmith结合三层结构模板,可以快速...

    asp.net codeSmith三层代码生成模板

    在CodeSmith生成的三层代码中,BLL类会调用DAL来访问数据库,执行必要的业务操作,如验证、计算、事务处理等。BLL的设计应遵循单一职责原则,每个类或方法只处理特定的业务逻辑。 3. 数据访问层(DAL): DAL负责...

    C#版 -- 实用性CodeSmith模板(自动生成三层架构底层代码)

    "C#版 -- 实用性CodeSmith模板(自动生成三层架构底层代码)"这个项目专注于为C#开发者提供一套定制的CodeSmith模板,用于快速构建三层架构的应用程序。模板的设计理念是减轻开发者手动编写基础架构代码的负担,让...

    .net三层模板 可以减轻代码量,适用于中、小型项目的代码生成 CodeSmith

    .NET三层架构模板是一种高效开发模式,它将应用分为表示层(UI)、业务逻辑层(BLL)和数据访问层(DAL)。这样的分层设计有助于提高代码的可读性、可维护性和可扩展性。CodeSmith是一款强大的代码生成工具,它可以...

    codesmith 3层代码生成模板

    Codesmith是一款基于.NET平台的代码生成工具,它通过读取数据库或其他数据源的信息,自动生成符合特定设计模式的源代码,如实体类、数据访问层、业务逻辑层等。它的核心优势在于可以根据不同的项目需求定制模板,...

    CodeSmith三层模板

    本资源“CodeSmith三层模板”专为构建典型的三层架构(表现层BLL、数据访问层DAL、实体模型Models)设计,旨在帮助开发者快速生成规范化的代码。 一、CodeSmith简介 CodeSmith是一款基于模板的代码生成工具,通过...

    CodeSmith企业版5.22+中文详细注释三层+抽象工厂模板+许可文件+中文件详细教程.part3

    当您生成应用程序时,您经常需要重复完成某些特定的任务,例如编写数据访问代码或者生成自定义集合。CodeSmith 在这些时候特别有用,因为您可以编写模板自动完成这些任务,从而不仅提高您的工作效率,而且能够自动...

    ASP.NET 自动同时生成三层架构代码 CodeSmith

    自动生成三层模型,及各表的基本方法的存储过程,执行完存储过程,并将三层代码COPY到相应解决方案文件夹下即可使用 3.执行时只需运行AllBaseModel.cst即可,然后选择数据库,Proname为项目名称

    CodeSmith简单三层模板

    这个“CodeSmith简单三层模板”很可能包含了一些预定义的模板文件,用于生成常见的三层架构代码,例如: - UI层的接口和实现类,可能包含控制器或视图模型。 - BLL层的业务实体类和业务逻辑处理类。 - DAL层的数据...

    SqlServer CodeSmith三层架构模版(升级版)

    在SQL Server环境中,利用CodeSmith可以快速生成与数据库交互的数据访问层(DAL)、业务逻辑层(BLL)以及表现层(UI)代码,构建出经典的三层架构模式。这种架构将数据处理、业务规则和用户界面分离,使得代码更...

    codesmith模板(MVC三层数据层模型层逻辑层)

    【codesmith模板(MVC三层数据层模型层逻辑层)】是软件开发中的一种高效工具,主要用于快速生成基于MVC架构的代码模板。MVC,即Model-View-Controller,是一种广泛采用的设计模式,用于分离应用程序的数据、表示和...

Global site tag (gtag.js) - Google Analytics