usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacethree-level linkage. model{classChina {Private stringCode; Public stringCode {Get{returnCode;} Set{Code =value;} } Private stringname; Public stringName {Get{returnname;} Set{name =value;} } Private stringParentcode; Public stringParentcode {Get{returnParentcode;} Set{Parentcode =value;} } }}
entity class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;namespacethree-level linkage. dataconnection{classDataConnection {Private Static stringConnstr="server=; database=mydb; user=sa; Pwd=ray ;"; Public StaticSqlConnection Conn {Get{return NewSqlConnection (CONNSTR);} } }}
Data Connection Class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;namespacethree-level linkage. dataoperation{classChinadata {PrivateSqlConnection _conn; PrivateSqlCommand _cmd; PrivateSqlDataReader _dr; PublicChinadata () {_conn=DataConnection.DataConnection.Conn; _cmd=_conn. CreateCommand (); } //return region information based on parent code PublicList<model.china> Select (stringPcode) {_cmd.commandtext="Select *from chinastates where [email protected]"; _cmd. Parameters.clear (); _cmd. Parameters.addwithvalue ("@pcode", Pcode); _conn. Open (); _DR=_cmd. ExecuteReader (); List<Model.China> list =NewList<model.china>(); if(_DR. HasRows) { while(_DR. Read ()) {Model.china data=NewModel.china (); Data. Code= _dr[0]. ToString (); Data. Name= _dr[1]. ToString (); Data. Parentcode= _dr[2]. ToString (); List. ADD (data); }} _conn. Close (); returnlist; } }}
Data Manipulation Classes
Visual Interface Operation:
Background code:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacethree-level linkage { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } dataoperation.chinadata CDA=NewDataoperation.chinadata (); //ways to fill the province Public voidFillsheng () {List<Model.China> list = cda. Select ("0001"); Cmbsheng.datasource=list; Cmbsheng.displaymember="Name"; Cmbsheng.valuemember="Code"; } //methods of filling the city Public voidFillshi () {List<Model.China> list =CDA. Select (CmbSheng.SelectedValue.ToString ()); Cmbshi.datasource=list; Cmbshi.displaymember="Name"; Cmbshi.valuemember="Code"; } //methods for populating the area Public voidFillqu () {List<Model.China> list =CDA. Select (CmbShi.SelectedValue.ToString ()); Cmbqu.datasource=list; Cmbqu.displaymember="Name"; Cmbqu.valuemember="Code"; } //Page One load run three methods Private voidForm1_Load (Objectsender, EventArgs e) {Fillsheng (); Fillshi (); Fillqu (); } //Fill City drop-down list Private voidCmbsheng_selectionchangecommitted (Objectsender, EventArgs e) {Fillshi (); Fillqu (); } //Drop -down list of populated areas Private voidCmbshi_selectionchangecommitted (Objectsender, EventArgs e) {Fillqu (); } }}
Exercise: WinForm Three-level linkage (Chinese administrative divisions)