This article mainly introduces the implementation of ASP.net based on anonymous class, DataTable, SQL generated entity classes, this small tool class is very practical, easy to use, need friends can refer to the
There are several scenarios that you might encounter in development:
1, the EF or LINQ query anonymous objects in other places to call inconvenient, and lazy manually build entity classes
2, through the DataTable reflection entities need to first build a class, headache
3, the entity returned through the SQL statement also need to build a class, headache
4, if the code generator to write templates, you need to install or do not want to generate a bunch of unused classes
In order to solve the above inconvenience, I encapsulated an entity generated class, can be thrown into the program arbitrary call
Encapsulation class:
?
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 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 5, 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 11 9 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>///* * Description: Entity Generation class///* Founder time: 2015-4-17///* * Modified time:-///* * Author: Sunkai Xuan///* * qq:610262374 Welcome to exchange, common improvement, naming grammar and other bad written places welcome everyone to give valuable suggestions///</summary> public class Classgenerating {///<su mmary>///get entity class strings based on anonymous classes///</summary>///<param name= "entity" > Anonymous objects </param>///<param name= " ClassName "> Generated class name </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>///get entity class strings based on DataTable///</summary>///<param name= "SQL" ></param& Gt <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>///Gets the string of entity classes based on SQL statements///</summary>///<param name= "SQL" >sql statement </param>// /<param name= "ClassName" > Generated class name </param>///<param name= "Server" > Service name </param>///<param Name= "Database" > Name </param>///<param name= "UID" > account </param>///<param name= "pwd" > Password </param>///<returns></returns> public static string Sqltoclass (String sql, String className, string s erver, string database, String uid, string pwd) {using (SqlConnection conn = new SqlConnection (String). Format ("Server={0};uid={2};p wd={3};d atabase={1}", Server, Database, UID, pwd)) {SqlCommand command = new SqlCommand (); Command. Connection = conn; Command.commandtext = SQL; DataTable dt = new DataTable (); SqlDataAdapter sad = new SqlDataAdapter (command); Sad. Fill (DT); var Reval = datatabletoclass (dt, className); return Reval; }///<summary>///gets the string of entity classes based on SQL statements///</summary>///<param name= "SQL" >sql statements </param>///< param name= "ClassName" > Generated class name </param>///<param name= "Connname" >webconfig connectionstrings /param>///<returns></returns> public static string Sqltoclass (String sql, String className, String connn AME) {string connstr = System.configuration.configurationmanager.connectionstrings[connname]. ToString (); if (connstr. Contains ("metadata"))//ef connstr = Regex.match (connstr, @ "Connection string=" (. +) "" "). GROUPS[1]. Value; using (SqlConnection conn = new SqlConnection (ConnStr)) {SqlCommand command = new SqlCommand () command. Connection = conn; Command.commandtext = SQL; DataTable dt = new DataTable (); SqlDataAdapter sad = new SqlDataAdapter(command); Sad. Fill (DT); var Reval = datatabletoclass (dt, className); return Reval; }///<summary>///match type///</summary>///<param name= "type" ></param>///<returns></ returns> private static string changetype (type type) {string typeName = type. Name; Switch (typeName) {case ' Int32 ': typeName = ' int '; break; case ' string ': TypeName = ' string '; break;} return typeName; } } } |
The call is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |
Entity classes are generated via anonymous objects var dynamicobj = new {id = 1, name = "nickname", Entity = new Enityt1 ()}; Note: Only a single entity cannot pass in List<t>, the collection requires list[0] string classcode = Classgenerating.dynamictoclass (Dynamicobj, " Classdynamic "); Generating entity class DataTable dt = new DataTable via DataTable (); Dt. Columns.Add ("Id", typeof (int)); Dt. Columns.Add ("Name"); Classcode = classgenerating.datatabletoclass (dt, "Classtatabale"); Generate entity classes via SQL statements Classcode = Classgenerating.sqltoclass ("SELECT * from note", "note", "127.0.0.1", "MyWork", "sa", "Sasa"); Classcode = Classgenerating.sqltoclass ("SELECT * FROM dbo.") Accessoriesdetail "," Accessoriesdetail "," nfdentities ");//through config connstring name |
Then in the debug state put the results you need CTRL + C then go to a new class Ctrl + V
The implementation of the