. NET three-tier architecture,. net three-tier architecture
Introduction to L3 architecture:
A three-tier architecture generally divides the entire Business application into User Interface layer and Business Logic Layer) and Data access layer ). The purpose of hierarchy differentiation is the idea of "high cohesion and low coupling. In the software architecture design, hierarchical structure is the most common and important structure. The hierarchical structure recommended by Microsoft is generally divided into three layers: data access layer, business logic layer (or domain layer), and presentation layer.
The procedure is as follows:
1. Create a new blank solution and set the name of the final solution.
2. Right-click the solution, select C # Language ==> class library, and set the class library name. For example, BookShop. MODEL (Data Interaction Layer ).
3. Repeat the second step to create the DLL layer and the BAL layer. For example, BookShop. BLL (business logic layer) and BookShop. DAL (data access layer ).
4. Right-click the solution and select C # Language ==> Windows form application, for example, BookShop. UI (Presentation Layer ).
5. delete BLL, DAL, and Class1 in the MODEL layer, right-click => Add => new item => class, and name it. (It is added after deletion for calling according to certain naming rules and will not misuse Class1 ).
The BLL layer ends with a Manager (for example, UserManager) and the DAL layer ends with a Service (for example, UserManager). The MODEL layer name is consistent with the table name in the database (for example, Users ).
6. Add references. The UI Layer references The bll model, The BLL layer references the dal model, and the DAL layer references the MODEL.
7. Create a SqlHelper class on the DAL layer for database connection.
8. The MODEL layer is mainly responsible for the value and value assignment of parameters. The parameters should correspond to columns in the database table.
public string UserId { get; set; } == private string UserId; public string id { get{ return ID; } set{ ID = value;} }
9. Three points are required for mutual calls of two classes: Add reference, declare namespace, and modify public.
10. write code first from the DAL layer, BLL layer, and UI Layer. (recommended)
11. Introduction to some ADO. NET objects
Connection: used to connect to a database. command: Execute SQL commands for the database. executeScalar (): returns the value of the first column of the First row (object type ). executeNonQuery (): returns the number of rows affected after the command is executed (int type ). executeReader (): the SQL statement and stored procedure of the returned result set, stored in the DataReader type object. dataReader: a feature that can be read from the beginning to the end and can be read row by row. sqlDataReader reader = cmd. executeReader (); while (reader. read () => put all data in reader {listBox1.Items. add (reader. getValue (0);} DataAdapter: You can use it to directly display tables! DataSet ds = new DataSet (); DataTable dt = new DataTable (); SqlDataAdapter adpt = new SqlDataAdapter (cmd); adpt. fill (ds); dt = ds. tables [0]; DataSet: a set of Tables. dataSet ds = new DataSet (); DataTable: a single table. dataTable dt = ds. tables [0] ;=> take the first table in DataSet; DataRow: Take a single row. dataRow dr = dt. rows [0] ;=> take the first row record in the dt table; SqlParameter: it can be understood as an array, and the elements of the array act as placeholders. string SQL = "select * from users where loginId = @ loginId and LoginPwd = @ pass"; SqlParameter [] pars = new SqlParameter [2]; ==> define two array lengths: pars [0] = new SqlParameter ("@ loginId", loginId ); ==> first element pars [1] = new SqlParameter ("@ pass", password); DataTable dt = SqlHelper. executeDataTable (SQL, CommandType. text, pars) ;=> the method to pass the parameter to the SqlHelper class ExecuteDataTable