using Model; using System.Collections.Generic; using System.Text; public class Class1 { #region 生成Model类 public void testff() { #region 数据库ID所对应的类型值 Dictionary<int, string> DicType = new Dictionary<int, string>(); DicType.Add(56, "int"); DicType.Add(231, "string"); #endregion #region 列名集合和数据ID Dictionary<string, int> DicColum = new Dictionary<string, int>(); DicColum.Add("name", 231);//名字 DicColum.Add("column_id", 56);//列ID DicColum.Add("system_type_id", 56); //系统类型 DicColum.Add("user_type_id", 56);//用户类型 DicColum.Add("max_length", 56); //最大长度 DicColum.Add("is_nullable", 56); //是否可空 #endregion GenerateModel(DicType, DicColum); }//传递参数GenerateModel(DIC,DIC); public void GenerateModel(Dictionary<int, string> DicType, Dictionary<string, int> DicColum) //生成Model类字符串 { string TypeName = "Model"; string TableName = "ColumnInfo"; //列名信息 TableName = TableName + TypeName; StringBuilder sb = new StringBuilder(); sb.Append("using System;"); //引入命名空间 sb.Append("namespace "); //命名空间名字 sb.Append(TypeName + "{"); sb.Append(" public partial class "); sb.Append(TableName + "{"); sb.Append("public " + TableName + "(){}"); sb.Append("#region Model" + " "); //Begin #region //内容 foreach (var i in DicColum) //增加字段 { foreach (var j in DicType) { if (i.Value == j.Key) // DicColum的56== DicType的56 { sb.Append("private "); sb.Append(j.Value); sb.Append(" _"); //sb.Append(i.Key); //DicColumd的DictID //转换一下大小写在追加 sb.Append(i.Key.ToLower()); sb.Append("; "); } } } foreach (var i in DicColum)//增加get/set { foreach (var j in DicType) { if (i.Value == j.Key) // DicColum的56== DicType的56 { sb.Append("public "); sb.Append(j.Value); sb.Append(" "); sb.Append(i.Key); sb.Append("{ "); //set{ _dicttype=value;} sb.AppendFormat("set{{ _{0} =value;}}", i.Key.ToLower()); sb.AppendFormat("get{{return _{0} ;}}", i.Key.ToLower()); sb.Append(" }"); } } } #endregion sb.Append(" " + "#endregion"); //End sb.Append(" " + "}}"); //sb.AppendFormat("using System;namespace {0}{ public partial class Dictionary{1}{ public {1}(){}}}"); string ss = sb.ToString(); } public void GenerateModel(List<ColumnInfoModel> list) { #region 数据库ID所对应的类型值 Dictionary<int, string> DicType = new Dictionary<int, string>(); DicType.Add(56, "int"); DicType.Add(231, "string"); #endregion string ClassName = "类名"; StringBuilder sb = new StringBuilder(); sb.Append("using System;"); //引入命名空间 sb.AppendFormat("namespace {0} {{ ", "Model"); sb.AppendFormat("public partial class {0} {{ ", ClassName); sb.AppendFormat("public {0}(){{}}", ClassName); //构造函数 sb.Append("#region Model" + " "); //Begin #region 内容 foreach (var i in list) { foreach (var j in DicType) { if (i.system_type_id == j.Key) { sb.Append("private "); sb.Append(j.Value); sb.Append(" _"); sb.Append(i.name.ToLower()); sb.Append("; "); } } } foreach (var i in list) { foreach (var j in DicType) { if (i.system_type_id == j.Key) { sb.Append("public "); sb.Append(j.Value); sb.Append(" "); sb.Append(i.name); sb.Append("{ "); //set{ _dicttype=value;} sb.AppendFormat("set{{ _{0} =value;}}", i.name.ToLower()); sb.AppendFormat("get{{return _{0} ;}}", i.name.ToLower()); sb.Append(" }"); } } } #endregion sb.Append(" " + "#endregion"); //End sb.Append(" " + "}}"); string ss = sb.ToString(); } //生成model类 字符串 #endregion #region 生成DAL类 public void GenerateDAL(string TableName, string ColumnName, string TypeName) { TypeName = "DAL"; TableName = "ClassName"; TableName = TableName + TypeName; StringBuilder sb = new StringBuilder(); sb.Append("using System;"); //引入命名空间 sb.Append("using System.Data;"); sb.Append("using System.Text;"); sb.Append("using System.Data.SqlClient;"); sb.Append("using System.Collections.Generic;"); sb.AppendFormat("namespace {0} { ", TypeName); sb.AppendFormat("public partial class {0} { ", TableName); sb.AppendFormat("public {0}(){}", TableName); //构造函数 sb.Append("#region --基本功能"); //基本功能 //--添加功能 sb.AppendFormat("public int Add({0} model)", TableName); sb.Append(" { "); sb.Append("StringBuilder strSql = new StringBuilder();"); sb.AppendFormat("strSql.Append("insert into {0}(");", TableName.Replace(TypeName, "")); sb.AppendFormat("{0}) values (", "DictType,ParentID,Subtitle,Sorting"); //列名 sb.AppendFormat("{0})", "@DictType,@ParentID,@Subtitle,@Sorting"); sb.Append(";select @@IDENTITY"); sb.Append("SqlParameter[] parameters = { "); sb.Append(" } "); sb.Append("#endregion");//end; sb.AppendFormat("public int Add({0} model) {{ ", "Model模型名称"); sb.Append("StringBuilder strSql = new StringBuilder();"); } public void GenerateDAL(List<ColumnInfoModel> list) { #region 数据库ID所对应的类型值 Dictionary<int, string> DicType = new Dictionary<int, string>(); DicType.Add(56, "int"); DicType.Add(231, "string"); #endregion string ClassName = "Dictionary";//类名 string ClassNameDAL = "Dictionary" + "DAL"; string ClassNameModel = "Dictionary" + "Model"; StringBuilder sb = new StringBuilder(); sb.Append("using System;"); //引入命名空间 sb.Append("using System.Data;"); sb.Append("using System.Text;"); sb.Append("using System.Data.SqlClient;"); sb.Append("using Model;"); sb.AppendFormat("namespace {0} {{ ", "DAL"); sb.AppendFormat("public partial class {0} {{ ", ClassNameDAL); sb.AppendFormat("public {0}(){{}} ", ClassNameDAL); //构造函数 sb.Append("#region --基本功能 "); //基本功能 #region //增加一个Model //增加一个Model sb.AppendFormat("public int Add({0} model)", ClassNameModel); sb.Append(" { "); sb.Append("StringBuilder strSql = new StringBuilder();"); sb.Append(" "); sb.AppendFormat("strSql.Append("insert into {0}(");", ClassName); sb.Append(" "); sb.AppendFormat("strSql.Append("{0})");", "DictType,ParentID,Subtitle,Sorting");//列名字符串 sb.Append(" "); sb.Append("strSql.Append(" values(");"); sb.Append(" "); sb.AppendFormat("strSql.Append("{0})");", "@DictType,@ParentID,@Subtitle,@Sorting)");//@列名字符串 sb.Append(" "); sb.Append("strSql.Append("; select @@IDENTITY");"); sb.Append(" "); sb.Append("SqlParameter[] parameters = { "); //// //new SqlParameter("@DictType", SqlDbType.NVarChar,50), foreach (var i in list) { foreach (var j in DicType) { if (i.system_type_id == j.Key)//DicColum的56== DicType的56 { if (j.Value == "int") { sb.Append(" "); sb.AppendFormat("new SqlParameter("@{0}", SqlDbType.{1},{2}),", i.name, "Int", i.max_length); sb.Append(" "); } if (j.Value == "string") { sb.Append(" "); sb.AppendFormat("new SqlParameter("@{0}", SqlDbType.{1},{2}),", i.name, "NVarChar", i.max_length / 2); sb.Append(" "); } } } } sb.Remove(sb.Length - 3, 3); sb.Append(" }; ");// SqlParameter[] parameters = {}; foreach (var i in list) { sb.AppendFormat("parameters[{0}].Value = model.{1};", list.IndexOf(i), i.name); sb.Append(" "); } sb.Append("object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);"); sb.Append(" "); sb.Append("if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } "); sb.Append(" } "); //--end #endregion #region 更新一条数据 //---终止 这种不灵活。。。。。 #endregion sb.Append("#endregion");//end; sb.Append(" } "); sb.Append(" } "); string ss = sb.ToString(); } #endregion }