I recently needed to query the database using SQL statements, but I didn't want to write the database every time, so I checked the reflection.
You want to use all of the properties that reflect an entity, and then query and assign values based on the property
First, you need an entity class to reflect the fields corresponding to the database.
But when I started to write properties, I saw that the features were pretty good, and that I could program the slices.
I also checked the characteristics, but the online information is not much, most of them are introduced, only their own slowly groping.
[Model.Context.SelectContext] public class Entity {public int ID {get; set;} public string Name {get; set;} }
Get an entity class, set a feature
Context.selectcontext
Can represent data that can be queried for
Next define the Sqlhelp class query
<span style= "White-space:pre" ></span>///<summary>///query Collection///</summary>// /<param name= "T" > Entity new () </param>//<param name= "SQL" >sql statement </param>//<PA Ram name= actions performed by "result" > Production entities </param>//<param name= "param" > Variable parameters </param>///<return S> returns the collection </returns> public static ienumerable<t> selectreader<t> (String sql, Func<system.data. Common.dbdatareader, t> result, params system.data.common.dbparameter[] param) where T:class { using (SqlConnection con = new SqlConnection (connstring)) {SqlCommand cmd = new Sqlcomman D (SQL, con); Cmd. Connection.Open (); if (param! = null && param. Length > 0) cmd. Parameters.addrange (param); SqlDataReader dr = cmd. ExecuteReader (); var list = new list<t> (); while (Dr. Read ()) {T T = result (DR);//generates an entity class delegate based on Sqldatereader to produce an entity class if (T! = NULL) list. ADD (t); } Dr. Close (); return list; } }
A query method that returns a collection based on the SQL statement
Next write a SQL helper class that defines a template for a SQL operation
attribute and reflection form SQL statement One