To load data from a database using List,dictionary

Source: Internet
Author: User

Scenario Description : There is a device table in the database, the field DWDM stores the factory number, the field ZNBH is the device number. DWDM and ZNBH are one-to-many relationships. The values in the database need to be loaded into list<dictionary<string,list<string>>.

One of the internal list<string> storage is a station number znbh,dictionary<string,list<string>> storage is a factory of DWDM and all the station number ZNBH,

list<dictionary<string,list<string>> are stored in the various plants of each unit.

The data in the database is as follows:

Case Study : According to test instructions, combined with the above specific data, we know that the database stored such data

List<string> list=new list<string> ()

{

"He", "the", "SL"

};

Dictionary<string,list<string>> dic=new dictionary<string,list<string>> ();

Dic.add ("30202419", list);

List<dictionary<string,list<string>>> result=new list<dictionary<string,list<string >>> ();

Result.add (DIC);

The key here is the time to create the list<string>-the first device in a factory, the time to add dic to the result set-the last device in a plant.

Code Implementation :

  Publiclist<dictionary<string, list<string>>> Getdwdmznbhlist (inttype) {List<Dictionary<string, list<string>>> result =NULL; stringsql =@"Select Dwdm,znbh from SYSCONFIG_LLWH where jclx={0} GROUP by DWDM, ZNBH order by DWDM"; SQL=string.format (SQL, type); DataTable DT=dbdataaccess.getinstance ().            Getdatatable (sysconfig.targetconnectionstring, SQL); stringDwdm_pre =String.Empty; stringDWDM =String.Empty; stringDwdm_next =String.Empty; stringZNBH =String.Empty; Dictionary<string, list<string>> dic =NULL; List<string> list =NULL; Result=Newlist<dictionary<string, list<string>>>();  for(inti =0; i < dt. Rows.Count; i++)            {                //assign a value to the unit code in front of the bank                if(i = =0) Dwdm_pre=""; ElseDwdm_pre= dt. Rows[i-1]["DWDM"].                ToString (); //assigning values to our unit codeDWDM = dt. rows[i]["DWDM"].                ToString (); //assign a value to the unit code of the line following US                if(I! = dt. Rows.Count-1) Dwdm_next= dt. Rows[i +1]["DWDM"].                ToString (); ElseDwdm_next=""; if(Dwdm_pre! =DWDM) {                    //Create list,dic When you plant your first deviceList =Newlist<string>(); DiC=Newdictionary<string, list<string>>(); DWDM= dt. rows[i]["DWDM"].                    ToString (); ZNBH= dt. rows[i]["ZNBH"].                    ToString (); List.                    ADD (ZNBH); Dic.                    ADD (DWDM, list); if(DWDM! =dwdm_next) result.                ADD (DIC); }                Else{ZNBH= dt. rows[i]["ZNBH"].                    ToString (); List.                    ADD (ZNBH); Dic.                    Clear (); Dic.                   ADD (DWDM, list); //The DIC is added to the list in the last device of the plant                    if(DWDM! =dwdm_next) result.                ADD (DIC); }            }            returnresult; }

To load data from a database using List,dictionary

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.