The magical DataReader of Dictionary data type

Source: Internet
Author: User

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

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.