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