DataReader to Dictionary has many uses, can output some fields in the table to the Entity field, previously required all the field output or another entity model to line, so you can reduce the output of the database, in particular, the format of some interface output is very convenient.
Look at the underlying code section first
/// <summary> ///DataReader Turn Dictionary<string, object>Data Type/// </summary> /// <param name= "DataReader" ></param> /// <param name= "Close" ></param> /// <returns></returns> Public Staticlist<dictionary<string,Object>> getlistdictionary (IDataReader dataReader,BOOLClose =true) {List<Dictionary<string,Object>> list =Newlist<dictionary<string,Object>>(); if(Close) {using(dataReader) { while(Datareader.read ()) {list. ADD (Datafilldictionary (DataReader)); } } } Else { while(Datareader.read ()) {list. ADD (Datafilldictionary (DataReader)); } } returnlist; } /// <summary> ///Convert DataReader data to Dictionary<string, object>Object/// </summary> /// <param name= "Reader" ></param> /// <returns></returns> Public Staticdictionary<string,Object>datafilldictionary (IDataReader reader) {Dictionary<string,Object> dict =Newdictionary<string,Object>(); for(inti =0; I < reader. FieldCount; i++) { Try{Dict. ADD (reader. GetName (i), reader. GetValue (i)); } Catch{Dict. ADD (reader. GetName (i),NULL); } } returndict; }
Call
Private voidGetwebuserlist (HttpContext context) {stringJsonresult =string. Empty; varModifiedOn = context. request["ModifiedOn"]; varList =Newlist<dictionary<string,Object>>(); using(varDBHelper =Dbhelperfactory.gethelper (Basesysteminfo.usercenterdbtype, basesysteminfo.usercenterdbconnection)) { Try { using(varDr =NewBaseusermanager (dbhelper). Getwebuserlist (ModifiedOn)) {if(Dr! =NULL) {List=Dblogic.getlistdictionary (DR); } } varTimeConverter =Newisodatetimeconverter {DateTimeFormat =Basesysteminfo.datetimeformat}; Jsonresult=jsonconvert.serializeobject (list, formatting.indented, timeconverter); } Catch(Exception ex) {Nloghelper.warn (ex,"Getwebuserlist Exception"); } finally{dbhelper.close (); } } }
Ways to get Data
/// <summary> ///Synchronize user information to official website///only partial fields are passed/// </summary> /// <returns></returns> PublicIDataReader Getwebuserlist (stringModifiedOn) { stringCommandText ="SELECT A."+Baseuserentity.fieldid+", A."+Baseuserentity.fieldusername+", A."+Baseuserentity.fieldrealname+", A."+Baseuserentity.fieldcode+", A."+Baseuserentity.fieldcompanyid+", A."+Baseuserentity.fieldcompanyname+", A."+Baseuserentity.fieldcompanycode+", B."+Baseusercontactentity.fieldmobile+", B."+Baseusercontactentity.fieldmodifiedon+" from"+ Baseuserentity.tablename +"A"+"Left JOIN"+ Baseusercontactentity.tablename +"B"+"On A."+ Baseuserentity.fieldid +"= B."+Baseusercontactentity.fieldid; //Don't give a record of time . if(!string. Isnullorwhitespace (ModifiedOn)) {varDbparameters =Newlist<keyvaluepair<string,Object>> ();//Query Condition Parameter collectionCommandText + ="WHERE B."+ Baseusercontactentity.fieldmodifiedon +"> To_date ("+ This. Dbhelper.getparameter (Baseusercontactentity.fieldmodifiedon +", ' Yyyy-mm-dd HH24:mi:ss ')"); //Songbiao, make a limit.CommandText + ="and ROWNUM <="; Dbparameters.add (Newkeyvaluepair<string,Object>(Baseusercontactentity.fieldmodifiedon, ModifiedOn)); return This. Dbhelper.executereader (CommandText, Dbhelper.makeparameters (dbparameters)); } return NULL; }
Output
The above output is the database field, we only need to change the query statement (field as Modelfield), you can also export the entity properties.
You can have a selection of some fields in the output table, this method adds a lot of benefits.
The magical DataReader of Dictionary data type