DAL <%-- Name:DAL Author: XuYabao Description: CREATE DAL --%> <%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Src="" Inherits="" Debug="False" Description="Template description here." %> <%@ Assembly Name="System.Data" %> <%@ Import Namespace="System.Data" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %> <%@ Property Name="Namespace" Type="String" Default="XUDAL" Category="Context" Description="Namespace." %> <%@ Property Name="DB" Type="String" Default="XUDB" Category="Context" Description="DB." %> <%@ Property Name="Model" Type="String" Default="XUModel" Category="Context" Description="Model." %> <%@ Property Name="IdentityColumn" Type="Int32" Default="0" Category="Context" Description="Is Return Identity. =0:No,>0:Yes" %> <%@ Property Name="Author" Type="String" Default="徐亚豹" Category="Context" Description="Author." %> <%@ Property Name="Description" Type="String" T Default="" Category="Context" Description="Description." Optional="true" %> <mce:script runat="template"><!-- public string GetDataType(ColumnSchema column) { string param = ""; switch (column.DataType) { case DbType.Int32: param += "int"; break; case DbType.Int64: param += "int"; break; case DbType.Double: param += "double"; break; case DbType.DateTime: param += "DateTime"; break; case DbType.Decimal: { param += "decimal"; break; } default: { param += "string"; break; } } return param; } // --></mce:script> using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using <%= DB %>; namespace <%= Namespace %> { /// <summary> /// 描述: <% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// 创建: <%= Author %> <%= DateTime.Now.ToString("yyyy-MM-dd") %> /// </summary> public class <%= SourceTable.Name.Substring(2) %> : DbBase { #region 获取<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// <summary> /// 描述: 获取<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// 创建: <%= Author %> <%= DateTime.Now.ToString("yyyy-MM-dd") %> /// </summary> <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %> /// <param name="<%= SourceTable.PrimaryKey.MemberColumns[i].Name.Substring(2) %>"><%= SourceTable.PrimaryKey.MemberColumns[i].Description %></param> <% } %> /// <returns></returns> public DataTable <%= SourceTable.Name.Substring(2) %>Get(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++){%><% if(i > 0){%>, <%}%><%= GetDataType(SourceTable.PrimaryKey.MemberColumns[i]) + " " +SourceTable.PrimaryKey.MemberColumns[i].Name.Substring(2)%><%}%>) { DataTable dt = null; SqlParameter[] parms = new SqlParameter[] { <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %> ParameterGet("@<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>", <%= SourceTable.PrimaryKey.MemberColumns[i].Name.Substring(2) %>, DbType.<%= SourceTable.PrimaryKey.MemberColumns[i].DataType %>,<% if(SourceTable.PrimaryKey.MemberColumns[i].DataType==DbType.String){%><%= SourceTable.PrimaryKey.MemberColumns[i].Size %>,<% } %> ParameterDirection.Input)<% if(i < SourceTable.PrimaryKey.MemberColumns.Count - 1 ) {%>,<% } %> <% } %> }; dt = DbExecute.ExecuteDataTable("p_<%= SourceTable.Name.Substring(2) %>Get", parms); return dt; } #endregion #region 新增<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// <summary> /// 描述: 新增<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// 创建: <%= Author %> <%= DateTime.Now.ToString("yyyy-MM-dd") %> /// </summary> /// <param name="mod">对象实例</param> /// <returns></returns> public int <%= SourceTable.Name.Substring(2) %>Add(<%= Model %>.<%= SourceTable.Name.Substring(2) %> mod) { int intRet = 0; <% if(IdentityColumn > 0){ %> string strRet; <% } %> SqlParameter[] parms = new SqlParameter[] { <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %> ParameterGet("@<%= SourceTable.Columns[i].Name %>", mod.<%= SourceTable.Columns[i].Name.Substring(2) %>, DbType.<%= SourceTable.Columns[i].DataType %>,<% if(SourceTable.Columns[i].DataType==DbType.String){%><%= SourceTable.Columns[i].Size %>,<% } %> ParameterDirection.<% if(i==IdentityColumn-1){%>Output<%}else{%>Input<%}%>)<% if(i < SourceTable.Columns.Count - 1 ) {%>,<% } %> <% } %> }; <% if(IdentityColumn > 0){ %> DbExecute.ExecuteNonQuery("p_<%= SourceTable.Name.Substring(2) %>Add", out strRet, parms); try { intRet = Convert.ToInt32(strRet); } catch { } <% }else{ %> intRet = DbExecute.ExecuteNonQuery("p_<%= SourceTable.Name.Substring(2) %>Add", parms); <% } %> return intRet; } #endregion #region 修改<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// <summary> /// 描述: 修改<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// 创建: <%= Author %> <%= DateTime.Now.ToString("yyyy-MM-dd") %> /// </summary> /// <param name="mod">对象实例</param> /// <returns></returns> public int <%= SourceTable.Name.Substring(2) %>Edit(<%= Model %>.<%= SourceTable.Name.Substring(2) %> mod) { int intRet = 0; SqlParameter[] parms = new SqlParameter[] { <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %> ParameterGet("@<%= SourceTable.Columns[i].Name %>", mod.<%= SourceTable.Columns[i].Name.Substring(2) %>, DbType.<%= SourceTable.Columns[i].DataType %>,<% if(SourceTable.Columns[i].DataType==DbType.String){%><%= SourceTable.Columns[i].Size %>,<% } %> ParameterDirection.Input)<% if(i < SourceTable.Columns.Count - 1 ) {%>,<% } %> <% } %> }; intRet = DbExecute.ExecuteNonQuery("p_<%= SourceTable.Name.Substring(2) %>Edit", parms); return intRet; } #endregion #region 删除<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// <summary> /// 描述: 删除<% if (SourceTable.Description.Trim().Length == 0) { %><%= SourceTable.Name.Substring(2) %><% }else{%><%= SourceTable.Description %><% } %> /// 创建: <%= Author %> <%= DateTime.Now.ToString("yyyy-MM-dd") %> /// </summary> <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %> /// <param name="<%= SourceTable.PrimaryKey.MemberColumns[i].Name.Substring(2) %>"><%= SourceTable.PrimaryKey.MemberColumns[i].Description %></param> <% } %> /// <returns></returns> public int <%= SourceTable.Name.Substring(2) %>Del(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++){%><% if(i > 0){%>, <%}%><%= GetDataType(SourceTable.PrimaryKey.MemberColumns[i]) + " " +SourceTable.PrimaryKey.MemberColumns[i].Name.Substring(2)%><%}%>) { int intRet = 0; SqlParameter[] parms = new SqlParameter[] { <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %> ParameterGet("@<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>", <%= SourceTable.PrimaryKey.MemberColumns[i].Name.Substring(2) %>, DbType.<%= SourceTable.PrimaryKey.MemberColumns[i].DataType %>,<% if(SourceTable.PrimaryKey.MemberColumns[i].DataType==DbType.String){%><%= SourceTable.PrimaryKey.MemberColumns[i].Size %>,<% } %> ParameterDirection.Input)<% if(i < SourceTable.PrimaryKey.MemberColumns.Count - 1 ) {%>,<% } %> <% } %> }; intRet = DbExecute.ExecuteNonQuery("p_<%= SourceTable.Name.Substring(2) %>Del", parms); return intRet; } #endregion } }