Mysqlhelper using the reflection mechanism to intelligently extrapolate data types and attribute names

Source: Internet
Author: User

     Public classMysqlhelper {Private stringconnstring;  PublicMysqlhelper (stringconnectionString) {connstring=connectionString; }         PublicIlist<t> runmysqlselect4returnlist<t> (stringstrcommand)whereT:New() {Mysqlcommand Mysqlcommand=NewMysqlcommand (); Mysqlcommand.commandtext=strcommand.tostring (); IList<PropertyInfo> Ilpropertyinfo =typeof(T). GetProperties (). ToList ();//P[0]. Name =travel_id;Ilist<t> Ilresult =NewList<t>(); DataTable DT=NewDataTable (); using(Mysqlconnection conn =NewMysqlconnection ( This. connstring)) {Conn.                Open (); using(Mysqlcommand cmd =Mysqlcommand) {cmd. Connection=Conn; using(Mysqldatareader RDR =cmd. ExecuteReader (commandbehavior.sequentialaccess)) {dt.                    Load (RDR); }                }            }            foreach(DataRow Drinchdt. Rows) {T TItem=NewT (); foreach(varVinchilpropertyinfo) {                    if(dt. Columns[v.name] = =NULL)Continue;                Convertionextensions.setvalue (TItem, V.name, Dr[v.name]);            } ilresult.add (TItem); }            returnIlresult; }         PublicT getsinggleobj<t> (stringstrcommand)whereT:New() {Mysqlcommand Mysqlcommand=NewMysqlcommand (); Mysqlcommand.commandtext=strcommand.tostring (); IList<PropertyInfo> Ilpropertyinfo =typeof(T). GetProperties (). ToList ();//P[0]. Name =travel_id;T Ilresult =NewT (); DataTable DT=NewDataTable (); using(Mysqlconnection conn =NewMysqlconnection ( This. connstring)) {Conn.                Open (); using(Mysqlcommand cmd =Mysqlcommand) {cmd. Connection=Conn; using(Mysqldatareader RDR =cmd. ExecuteReader (commandbehavior.sequentialaccess)) {dt.                    Load (RDR); }                }            }            if(dt. Rows.Count <=0)            {                return default(T); }            Else            {                foreach(DataRow Drinchdt. Rows) {T TItem=NewT (); foreach(varVinchilpropertyinfo) {                        if(dt. Columns[v.name] = =NULL)Continue;                    Convertionextensions.setvalue (TItem, V.name, Dr[v.name]); } Ilresult=TItem; }            }            returnIlresult; }         PublicIlist<t> runmysqlselect4returnlist<t> (DataTable dts)whereT:New() {IList<PropertyInfo> Ilpropertyinfo =typeof(T). GetProperties (). ToList ();//P[0]. Name =travel_id;Ilist<t> Ilresult =NewList<t>(); DataTable DT=DTS; foreach(DataRow Drinchdt. Rows) {T TItem=NewT (); foreach(varVinchilpropertyinfo) {                    if(dt. Columns[v.name] = =NULL)Continue;                Convertionextensions.setvalue (TItem, V.name, Dr[v.name]);            } ilresult.add (TItem); }            returnIlresult; }    }     Public Static classconvertionextensions { Public StaticT convertto<t> ( Thisiconvertible Convertiblevalue) {            vart =typeof(T); if(NULL==convertiblevalue) {                return default(T); }            if(!typeof(T). Isgenerictype) {returnT Convert.changetype (Convertiblevalue,typeof(T)); }            Else{Type generictypedefinition=typeof(T).                GetGenericTypeDefinition (); if(Generictypedefinition = =typeof(nullable<>))                {                    returnT Convert.changetype (Convertiblevalue, Nullable.getunderlyingtype (typeof(T))); }            }            Throw NewInvalidCastException (string. Format ("Invalid cast from type \ "{0}\" to type \ "{1}\".", Convertiblevalue.gettype (). FullName,typeof(T).        FullName)); }         Public Static voidSetValue (ObjectInputobject,stringPropertyName,Objectpropertyval) {            //find out the typeType type =Inputobject.gettype (); //get the property information based on the typeSystem.Reflection.PropertyInfo PropertyInfo =type.            GetProperty (PropertyName); //Find the property typeType PropertyType =Propertyinfo.propertytype; //Convert.changetype does not handle conversion to nullable types//If the property type was nullable, we need to get the underlying type of the property            varTargetType = Isnullabletype (propertyinfo.propertytype)?Nullable.getunderlyingtype (propertyinfo.propertytype): Propertyinfo.propertytype; //Returns an System.Object with the specified System.Type and whose value is//equivalent to the specified object.Propertyval =Convert.changetype (Propertyval, TargetType); //Set The value of the propertyPropertyinfo.setvalue (Inputobject, Propertyval,NULL); }        Private Static BOOLisnullabletype (Type type) {returnType. Isgenerictype && type. GetGenericTypeDefinition (). Equals (typeof(nullable<>)); }    }

Mysqlhelper using the reflection mechanism to intelligently extrapolate data types and attribute names

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.