In Asp.net, retrieving Entity Data Using Generics can improve efficiency and simplify the Code. This example uses a three-tier architecture. First, define the stuinfo object in the model layer, and then define the list <> generic query database in the sqlhelper data operation class of the Dal layer to obtain the object data, and finally call it through the BLL layer method. The specific example is as follows:
I. stuinfo objects defined in the model layer:
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace model {public class stuinfo {private string _ id; Public String ID {get {return _ id;} set {_ id = value ;}} private string _ stuname; public String stuname {get {return _ stuname;} set {_ stuname = value ;}} private string _ stuclass; Public String stuclass {get {return _ stuclass ;} set {_ stuclass = value ;}}}}
2. Obtain object data through list generics in the Dal layer:
Public list <stuinfo> date () {list <stuinfo> List = NULL; cmd = new sqlcommand ("select * From stuinfo", Conn); try {Conn. open (); DR = cmd. executereader (); List = new list <stuinfo> (); While (dr. read () {stuinfo Stu = new stuinfo (); Stu. id = Dr ["ID"]. tostring (); Stu. stuname = Dr ["stuname"]. tostring (); Stu. stuclass = Dr ["stuclass"]. tostring (); list. add (Stu);} return list;} catch (exception) {return NULL;} finally {Conn. close ();}}
3. Call the Dal Layer Method in The bll layer to obtain the object data:
Using system; using system. collections. generic; using system. LINQ; using system. text; using Dal; using model; namespace BLL {public class studel {sqlhelper DB = new sqlhelper (); public list <stuinfo> date () {return dB. date ();}}}
4. Display Entity Data in the Web UI Layer
After step 3, the object data has been added to the list generic set. How can this be displayed? You can add a data display control on the page, such as the gridview, and then specify a data source for it in the CS code. This list generic set can be used as its data source to display data.
Using system; using system. configuration; using system. data; using system. LINQ; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. XML. LINQ; using BLL; public partial class _ default: system. web. UI. page {studel de = new studel (); protected void page_load (Object sender, eventargs e) {} protected void button2_click (Object sender, eventargs e) {gridview1.datasource = de. date (); gridview1.databind ();}}
This example has been debugged by the author.
(This article from small talk blog http://tanteng.sinaapp.com, reprint please indicate the source !)