attribute and reflection form SQL statement One

Source: Internet
Author: User

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>//&LT;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

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.