Convert an entity class, an anonymous object, to a SqlParameter list

Source: Internet
Author: User

<summary>///<remarks>///<para> Convert entity class/anonymous object to SqlParameter list </para>///<para> Example 1: Entity class </para>///<para>entityclass Entityclass = new Entityclass () {Id = 1, name= "name"}; sqlparameter[] parms = Entityclass.tosqlparameterarray () </para>///<para> Example 2: Anonymous Object </para>///< Para>var Anonymousobject = new {Id = 1, name= "name"}; sqlparameter[] parms = Anonymousobject.tosqlparameterarray () </para>///<para> Note: SqlParameter (string The parameter parametername of ParameterName, object value) is a lowercase copy of the entity class/anonymous object property. </para>///<para> Therefore, the corresponding SQL parameter should be lowercase, for example: INSERT into Tbname (name) values (@name) </para>///</ remarks>///</summary>///<param name= "obj" > entity class/Anonymous object </param>///<returns> SqlParameter parameter array </returns>public static sqlparameter[] tosqlparameterarray<t> (This T entityClass) where T    : class{list<sqlparameter> parms = new list<sqlparameter> (); propertyinfo[] Propertys = EntiTyclass.gettype ().    GetProperties (); foreach (PropertyInfo pi in propertys) {//Determines if this property has a getter if (!pi.        CanRead) {continue; } Object value = Pi.        GetValue (Entityclass); Parms. ADD (New SqlParameter ("@" + pi).    Name.tolower (), value)); } return parms. ToArray ();} /*>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>    >>>>>>>>>>>>>>>*/public class entityclass{public int Id {get; set;}    public string Name {get; set;} public int Age {get; set;}} Use example://1. Entity class Entityclass EC = new Entityclass () {name= "mammoth", age = 2};//sqlparameter[] parms = EC. Tosqlparameterarray (); var parms = ec. Tosqlparameterarray ();//2.Anonymous class var EC = new{Name = "woolly elephant", age = "2"};//sqlparameter[] parms = EC. Tosqlparameterarray (); var parms = ec. Tosqlparameterarray ();

  

Convert an entity class, an anonymous object, to a SqlParameter list

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.