1.
Relationship between three layers:
Layer 3 refers : Ui, business, and data access)
Text description : Clients performs operations on the UI. The UI calls business to perform operations and processing. Business uses data access to perform operations on data base.
Advantages : L Added Code . Data access can be shared among multiple projects; business can be used in different places of the same project (for example, a software B/S and C/S can share a series of business components ). L This makes software layers clearer and easier to develop and maintain. The artist can easily design the uidesign and call the interfaces provided by business. Program Developers can focus on coding and implementing functions.
2. Data Access
Specific implementation: Description of variables and methods in the dataagent type: Private string m_strconnectionstring; // connection string private oledbconnection m_objconnection; // database connection public dataagent (string strconnection) // constructor, the input parameter is the connection string private void opendatabase () // open the database connection private void # region closedatabase () // close the database connection public dataview getdataview (string strsqlstat) // return dataview based on the input connection string. The specific implementation code is as follows: Public Class Dataagent { Private StringM_strconnectionstring; Private OledbconnectionM_objconnection; # Region Dataagend ///<Summary> ///Initial Function ///</Summary> ///<Param name = "strconnection"> </param> PublicDataagent (StringStrconnection) { This. M_strconnectionstring = strconnection; } # Endregion # Region Opendatabase ///<Summary> ///Open Database ///</Summary> Private VoidOpendatabase () { Try { This. M_objconnection =New Oledbconnection(); This. M_objconnection.connectionstring =This. M_strconnectionstring; If(This. M_objconnection.state! =Connectionstate. Open) { This. M_objconnection.open (); } } Catch(ExceptionE) { ThrowE; } } # Endregion # Region Closedatabase ///<Summary> ///Close Database ///</Summary> Private VoidClosedatabase () { If(This. M_objconnection! =Null) { If(This. M_objconnection.state =Connectionstate. Open) { This. M_objconnection.close (); } } } # Endregion # Region Getdataview ///<Summary> ///Execute the SQL and return the default table View ///</Summary> ///<Param name = "strselectstring">Select string</Param> ///<Returns>Dataview of the datatable</Returns> Public DataviewGetdataview (StringStrsqlstat) { Try { This. Opendatabase (); OledbdataadapterObjdataadapter =New Oledbdataadapter(Strsqlstat. Trim (),This. M_objconnection ); DatasetObjdataset =New Dataset(); Objdataadapter. Fill (objdataset ); ReturnObjdataset. Tables [0]. defaultview; } Catch(ExceptionE) { ThrowE; } Finally { This. Closedatabase (); } } # Endregion }
3. Business
Specific implementation: Create a class named base. This class serves as the base class of other transaction classes, and defines a dataagent instance. All other business classes are derived from the modified class. Add a reference to dataagent in this class so that all transaction classes can use the methods in dataagent. Base. CS Source code : Public Abstract Class Base { Protected DataagentOledbagent =New Dataagent("Provider = sqloledb; Data Source = (local); database = test; user id = sa; Pwd ="); } After preparing the basic classes of the data operation layer and Transaction layer, you can officially start the development of the business logic class. For example, there is a news class that displays news, including a getnewslist () to obtain the list of all news titles. The Code is as follows: Public Class News:Base { Public DataviewGetnewslist () { StringStrsql; Strsql =""; Strsql + ="Select top 10 newsid, newstitle"; Strsql + ="From tb_news"; Strsql + ="Where newsenable = 1"; Strsql + ="Order by newsid"; ReturnOledbagent. getdataview (strsql ); } } Because the database structure is relatively simple, no detailed table structure is provided here.
4. UI
Layer-to-business
Interface call First, add a reference to the news class in form form1. Then, add a (datagridview) dgnews in the form form1 to display the news list. In the form Form1_load Add the following code to the method: Private VoidForm1_load (ObjectSender,EventargsE) { NewsObjnews =New News(); This. Dgnews. datasource = objnews. getnewslist (); }