<%@ CodeTemplate Language="C#" ClassName="MvcDAL" TargetLanguage="C#" Debug="False" Description="" %> <%@ Property Name="databaseProjectName" Type="System.String" Default="SpSampleDb" Optional="True" Category="Context" Description="" %> <%@ Property Name="nameSpace" Type="System.String" Default="Dal" Optional="True" Category="Context" Description="" %> <% DbEntity db = Template.GetDataBase(databaseProjectName); foreach(Table tb in db.Tables) { OpenResponse(databaseProjectName+"\\MvcDAL\\"+tb.ModuleName+"\\"+tb.PropertyName+"Dal.cs"); string noDefaultFields = ""; string noDefaultFieldFormats = ""; string noDefaultFieldValues = ""; int iField = 0; foreach(Field fd in tb.Fields) { if( (fd.DefaultValue != "" || fd.DefaultValue != null) && fd.Identity.Enable == false) { noDefaultFields += "\t\t\t\t\t"+fd.MemberName+",\r\n"; if( fd.DataType.GroupName.ToUpper() == "STRING") noDefaultFieldValues += "\t\t\t\t\tFormatCom.String("+tb.MemberName+"."+fd.MemberName+"),\r\n"; else if( fd.DataType.GroupName.ToUpper() == "DATETIME") noDefaultFieldValues += "\t\t\t\t\tDBAccess.DateStr("+tb.MemberName+"."+fd.MemberName+"),\r\n"; else noDefaultFieldValues += "\t\t\t\t\t"+tb.MemberName+"."+fd.MemberName+",\r\n"; if( fd.DataType.GroupName.ToUpper() == "NUMBER") noDefaultFieldFormats += "{"+iField.ToString()+"},"; //else if( fd.DataType.GroupName.ToUpper() == "DATETIME") // noDefaultFieldFormats += "{"+iField.ToString()+"},"; else noDefaultFieldFormats += "'{"+iField.ToString()+"}',"; iField++; } } if( noDefaultFields.EndsWith(",\r\n")) noDefaultFields = noDefaultFields.Substring(0,noDefaultFields.Length-3); if( noDefaultFieldValues.EndsWith(",\r\n")) noDefaultFieldValues = noDefaultFieldValues.Substring(0,noDefaultFieldValues.Length-3); noDefaultFieldFormats = noDefaultFieldFormats.Trim(','); %> using System; using System.Data; using System.Collections.Generic; using Mobile.Model; /*******************************************************/ /*Project: Module : Description : Date : <%=DateTime.Now.ToString()%> Create : lxc Update : TODO : */ /*******************************************************/ namespace Mobile.<%=nameSpace%> { public class <%=tb.PropertyName%>Dal { public <%=tb.PropertyName%>Dal() { } #region Insert public static int Insert(<%=tb.PropertyName%>Model <%=tb.MemberName%>) { string sql = @"Insert Into tab<%=tb.MemberName%> ( <%=noDefaultFields%> ) values(<%=noDefaultFieldFormats%>)"; sql = string.Format(sql, <%=noDefaultFieldValues%> ); return DBAccess.ExecuteSql(sql); } #endregion #region Update public static int Update(<%=tb.PropertyName%>Model <%=tb.MemberName%>) { string sql = @"Update tab<%=tb.MemberName%> set <% iField = 0; string updstr = ""; string upvalue = ""; iField = 0; bool bHasPrevPk = false; foreach(Field fd in tb.Fields) { if( fd.Identity.Enable == false && (fd.IsPK == false || bHasPrevPk== true)) { updstr += "\t\t\t\t\t"+fd.MemberName+"= "; if( fd.DataType.GroupName.ToUpper() == "NUMBER") updstr += "{"+iField.ToString()+"}"; //else if( fd.DataType.GroupName.ToUpper() == "DATETIME") // updstr += "{"+iField.ToString()+"}"; else updstr += "'{"+iField.ToString()+"}'"; updstr += ",\r\n"; if( fd.DataType.GroupName.ToUpper() == "STRING") upvalue += "\t\t\t\t\tFormatCom.String("+tb.MemberName+"."+fd.MemberName+"),\r\n"; else if( fd.DataType.GroupName.ToUpper() == "DATETIME") upvalue += "\t\t\t\t\tDBAccess.DateStr("+tb.MemberName+"."+fd.MemberName+"),\r\n"; else upvalue += "\t\t\t\t\t"+tb.MemberName+"."+fd.MemberName+",\r\n"; iField++; } if( fd.IsPK == true) bHasPrevPk = true; } if( updstr.EndsWith(",\r\n")) updstr = updstr.Substring(0,updstr.Length-3); %> <%=updstr%> where <% updstr = ""; foreach(Field fd in tb.Fields){ if( fd.IsPK == true) { updstr += "\t\t\t\t\t"+fd.PropertyName+"= "; if( fd.DataType.GroupName.ToUpper() == "NUMBER") updstr += "{"+iField.ToString()+"}"; //else if( fd.DataType.GroupName.ToUpper() == "DATETIME") // updstr += "{"+iField.ToString()+"}"; else updstr += "'{"+iField.ToString()+"}'"; updstr += ",\r\n"; if( fd.DataType.GroupName.ToUpper() == "DATETIME") upvalue += "\t\t\t\t\tDBAccess.DateStr("+tb.MemberName+"."+fd.MemberName+"),\r\n"; else upvalue += "\t\t\t\t\t"+tb.MemberName+"."+fd.MemberName+",\r\n"; break; }} if( updstr.EndsWith(",\r\n")) updstr = updstr.Substring(0,updstr.Length-3); if( upvalue.EndsWith(",\r\n")) upvalue = upvalue.Substring(0,upvalue.Length-3); %> <%=updstr%>"; sql = string.Format(sql, <%=upvalue%>); return DBAccess.ExecuteSql(sql); } #endregion <% string signPk = ""; string signPkValue = ""; string pkList = ""; string pkListValue = ""; foreach(Field fd in tb.Fields) { if( fd.IsPK == true) { pkList += fd.DataType.DotNetType+" "+ fd.MemberName+","; pkListValue += "\t\t\t\t\t"+fd.MemberName+",\r\n"; if( signPk == "") { signPk = fd.DataType.DotNetType+" "+ fd.MemberName; signPkValue = "\t\t\t"+fd.MemberName ;//+"\r\n"; } } } pkList = pkList.Trim(','); if( pkListValue.EndsWith(",\r\n")) pkListValue = pkListValue.Substring(0,pkListValue.Length-3); %> #region Delete public static int Delete(<%=signPk%>) { <% string delInsstr = ""; string selectInsstr = ""; string signSelectInsstr = ""; string delIncvalue = ""; iField = 0; foreach(Field fd in tb.Fields) { if( fd.IsPK == true) { string strt = ""; if( fd.DataType.GroupName.ToUpper() == "NUMBER") strt = "{"+iField.ToString()+"}"; else strt = "'{"+iField.ToString()+"}'"; if( signSelectInsstr == "") signSelectInsstr = "\t\t\t\t\ta."+fd.PropertyName+"= " + strt +"\r\n"; selectInsstr += "\t\t\t\t\ta."+fd.PropertyName+"= " + strt +",\r\n"; delInsstr += "\t\t\t\t\t"+fd.PropertyName+"= " + strt +",\r\n"; delIncvalue += "\t\t\t"+tb.MemberName+"."+fd.PropertyName+",\r\n"; iField++; break; } } if( selectInsstr.EndsWith(",\r\n")) selectInsstr = selectInsstr.Substring(0,selectInsstr.Length-3); if( delInsstr.EndsWith(",\r\n")) delInsstr = delInsstr.Substring(0,delInsstr.Length-3); if( delIncvalue.EndsWith(",\r\n")) delIncvalue = delIncvalue.Substring(0,delIncvalue.Length-3); %> string sql = @"Delete from tab<%=tb.MemberName%> where <%=delInsstr%>"; sql = string.Format(sql,<%=signPkValue%>); return DBAccess.ExecuteSql(sql); } #endregion #region Select <% string fieldstr = "a.*"; string tablestr = tb.Name+" a "; foreach(Field fd in tb.Fields) { FieldRel fieldRel = new FieldRel(fd.Code); if( fieldRel.RelTable != "") { fieldstr +=","+fieldRel.RelTableAlias+"."+fieldRel.RelShowField; if( fieldRel.RelShowFieldAlias != fieldRel.RelShowField) fieldstr += " as "+fieldRel.RelShowFieldAlias; tablestr +=" inner join "+fieldRel.RelTable; if( fieldRel.RelTableAlias != fieldRel.RelTable) tablestr += " "+fieldRel.RelTableAlias; tablestr +=" on a."+fd.PropertyName+"="+fieldRel.RelTableAlias+"."+fieldRel.RelField; } } string sqlHead = "select "+fieldstr+" from "+tablestr; %> public static DataSet Select() { string sql = "<%=sqlHead%>"; return DBAccess.Query(sql); } public static DataSet Select(string where) { string sql = "<%=sqlHead%>"; if( where !="") sql += " where "+ where; return DBAccess.Query(sql); } public static List<<%=tb.PropertyName%>Model> SelectModelList(string where) { List<<%=tb.PropertyName%>Model> modelLst = new List<<%=tb.PropertyName%>Model>(); DataSet ds = Select(where); if (DBUnit.HasRow(ds)) { DataTable dt = ds.Tables[0]; int icount = dt.Rows.Count; for (int i = 0; i < icount; i++) { DataRow dr = dt.Rows[i]; <%=tb.PropertyName%>Model messagemodel = new <%=tb.PropertyName%>Model(dr); modelLst.Add(messagemodel); } } return modelLst; } public static List<<%=tb.PropertyName%>Model> SelectModelList(string where,int pageIndex,int pageSize,out int count ) { List<<%=tb.PropertyName%>Model> modelLst = new List<<%=tb.PropertyName%>Model>(); DataSet ds = SelectForList(where,pageIndex,pageSize,out count); if (DBUnit.HasRow(ds)) { DataTable dt = ds.Tables[0]; int icount = dt.Rows.Count; for (int i = 0; i < icount; i++) { DataRow dr = dt.Rows[i]; <%=tb.PropertyName%>Model messagemodel = new <%=tb.PropertyName%>Model(dr); modelLst.Add(messagemodel); } } return modelLst; } public static DataSet SelectForList(string where,int pageIndex,int pageSize,out int count ) { count = 0; string tableName ="V_<%=tb.Name%>"; return DBAccess.Query(tableName,pageSize,pageIndex, where,ref count); } public static <%=tb.PropertyName%>Model SelectByWhere(string where) { <%=tb.PropertyName%>Model <%=tb.MemberName%> = null; DataSet ds = Select(where); if (DBUnit.HasRow(ds)) <%=tb.MemberName%> = new <%=tb.PropertyName%>Model(ds.Tables[0].Rows[0]); return <%=tb.MemberName%>; } public static <%=tb.PropertyName%>Model Select(Int32 id) { string where = "id="+id; return SelectByWhere(where); } #endregion } } <% CloseResponse(); } %>