您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp.net编程 >> 正文

ASP.NET中实现根据匿名类、datatable、sql生成实体类

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了ASP.NET中实现根据匿名类、datatable、sql生成实体类,这个小小工具类非常实用,使用起来也很方便,需要的朋友可以参考下

  在开发中可能会遇到这几种情况:

  1、EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类

  2、通过datatable反射实体需要先建一个类 ,头痛

  3、通过SQL语句返回的实体也需要先建一个类 ,头痛

  4、如果通过代码生成器要写模版,需要安装或者不想生成一堆不用的类

  为了解决上面的不便之处,我封装了一个实体生成类,可以扔到程序里面任意调用

  封装类:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Text.RegularExpressions;   namespace SyntacticSugar { /// <summary> /// ** 描述:实体生成类 /// ** 创始时间:2015-4-17 /// ** 修改时间:- /// ** 作者:sunkaixuan /// ** qq:610262374 欢迎交流,共同提高 ,命名语法等写的不好的地方欢迎大家的给出宝贵建议 /// </summary> public class ClassGenerating { /// <summary> /// 根据匿名类获取实体类的字符串 /// </summary> /// <param name="entity">匿名对象</param> /// <param name="className">生成的类名</param> /// <returns></returns> public static string DynamicToClass(object entity, string className) { StringBuilder reval = new StringBuilder(); StringBuilder propertiesValue = new StringBuilder(); var propertiesObj = entity.GetType().GetProperties(); string replaceGuid = Guid.NewGuid().ToString(); string nullable = string.Empty; foreach (var r in propertiesObj) {   var type = r.PropertyType; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { type = type.GetGenericArguments()[0]; nullable = "?"; } if (!type.Namespace.Contains("System.Collections.Generic")) { propertiesValue.AppendLine(); string typeName = ChangeType(type); propertiesValue.AppendFormat("public {0}{3} {1} {2}", typeName, r.Name, "{get;set;}", nullable); propertiesValue.AppendLine(); } }   reval.AppendFormat(@" public class {0}{{ {1} }} ", className, propertiesValue);     return reval.ToString(); }     /// <summary> /// 根据DataTable获取实体类的字符串 /// </summary> /// <param name="sql"></param> /// <param name="className"></param> /// <returns></returns> public static string DataTableToClass(DataTable dt, string className) { StringBuilder reval = new StringBuilder(); StringBuilder propertiesValue = new StringBuilder(); string replaceGuid = Guid.NewGuid().ToString(); foreach (DataColumn r in dt.Columns) { propertiesValue.AppendLine(); string typeName = ChangeType(r.DataType); propertiesValue.AppendFormat("public {0} {1} {2}", typeName, r.ColumnName, "{get;set;}"); propertiesValue.AppendLine(); } reval.AppendFormat(@" public class {0}{{ {1} }} ", className, propertiesValue);     return reval.ToString(); }   /// <summary> /// 根据SQL语句获取实体类的字符串 /// </summary> /// <param name="sql">SQL语句</param> /// <param name="className">生成的类名</param> /// <param name="server">服务名</param> /// <param name="database">数据库名称</param> /// <param name="uid">账号</param> /// <param name="pwd">密码</param> /// <returns></returns> public static string SqlToClass(string sql, string className, string server, string database, string uid, string pwd) { using (SqlConnection conn = new SqlConnection(string.Format("server={0};uid={2};pwd={3};database={1}", server, database, uid, pwd))) { SqlCommand command = new SqlCommand(); command.Connection = conn; command.CommandText = sql; DataTable dt = new DataTable(); SqlDataAdapter sad = new