. Net generates object classes based on anonymous classes, generates object classes based on datatable, and generates object classes based on SQL. netdatatable

Source: Internet
Author: User

. Net generates object classes based on anonymous classes, generates object classes based on datatable, and generates object classes based on SQL. netdatatable

These situations may occur during development.

1. The anonymous objects queried by EF or LINQ are not convenient to call in other places, and they are also lazy to manually create object classes.

2. You need to create a class to reflect objects through datatable.

3. You also need to create a class for the entities returned by SQL statements.

4. If you want to write a template through the Code Generator, you need to install or do not want to generate a bunch of unused classes.

 

To solve the above inconvenience, I encapsulated an entity generation class, which can be thrown into the program for arbitrary calls.

 

Encapsulation class:

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 // ** Creation Time: // ** modification time: -/// ** Author: sunkaixuan // ** qq: 610262374. Welcome to join us for further improvement, naming syntax and other poorly written information. You are welcome to give valuable suggestions. /// </summary> public class ClassGenerating {// <summary> // obtain the Object class Based on the anonymous class </summary> /// <param name = "entity"> anonymous object </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 (); foreach (var r in propertiesObj) {propertiesValue. appendLine (); propertiesValue. appendFormat ("public {0} {1} {2}", r. propertyType. fullName, r. name, "{get; set;}"); propertiesValue. appendLine ();} reval. appendFormat (@ "public class {0 }{{ 1 }}", className, propertiesValue); return reval. toString ();} /// <summary> /// obtain the object class String Based on the able /// </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 (); propertiesValue. appendFormat ("public {0} {1} {2}", r. dataType, r. columnName, "{get; set;}"); propertiesValue. appendLine ();} reval. appendFormat (@ "public class {0 }{{ 1 }}", className, propertiesValue); return reval. toString ();} /// <summary> /// obtain the object class String Based on the SQL statement // </summary> /// <param name = "SQL"> SQL statement </param> /// <param name = "className"> generated class name </param> /// <param name = "server"> service name </param> /// <param name = "database"> database name </param> /// <param name = "uid"> account </param> /// <param name = "pwd"> password </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 SqlDataAdapter (command); sad. fill (dt); var reval = DataTableToClass (dt, className); return reval ;}} /// <summary> /// obtain the object class String Based on the SQL statement // </summary> /// <param name = "SQL"> SQL statement </param> /// <param name = "className"> generated class name </param> /// <param name = "connName"> connectionStrings name of webconfig </param>/ // <returns> </returns> public static string SqlToClass (string SQL, string className, string connName) {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 ;}}}}

 

 

The call is as follows:

// Generate an object class var dynamicObj = new {id = 1, name = "", entity = new enityt1 ()}; string classCode = ClassGenerating. dynamicToClass (dynamicObj, "classDynamic"); // generate an object class datatable dt = new DataTable (); dt. columns. add ("Id", typeof (int); dt. columns. add ("Name"); classCode = ClassGenerating. dataTableToClass (dt, "classTatabale"); // generate object class classCode = ClassGenerating using SQL statements. sqlToClass ("select * from note", "Note", "127.0.0.1", "MyWork", "sa", "sasa"); classCode = ClassGenerating. sqlToClass ("select * from dbo. accessoriesDetail "," AccessoriesDetail "," NFDEntities "); // use the config connstring name

 

Then, in the debugging status, CTRL + C the desired result and create a new class CTRL + V.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.