C # layered three-tier architecture Very Good !,

Source: Internet
Author: User

C # layered three-tier architecture Very Good !,

Hello! Welcome new and old friends to join us. We are always waiting for you.

Next, let's talk about the three-tier architecture.BytesBytesBytesBytesBytesBytes

The three-layer architecture includes the presentation layer (UI) and business logic layer(BLL (Business Logic Layer ))Data Access Layer (DAL) and Object Library (Model)

1. The object library (Model) Stores Table fields in the database.

Operation:

(1) first, create an object class library Model, open the project, right-click solution> Add> Create Project> select class library> rename Model> OK

(2) Select the Model class library --> Shift + ALT + C --> Create an object class. UserInfo class

1 namespace Model2 {3    public  class UserInfo4     {5         public string  UserName { get; set; }6         public string  Password { get; set; }7     }8 }

2. Data access layer (DAL) is mainly used to store access to data classes, that is, basic operations such as adding, deleting, modifying, and updating databases.

 

Operation:

 

(1) first, create the data access layer class library DAL, open the project, right-click the solution, and choose add> new project> select class library> rename DAL> OK.

(2) Add a reference to the Model in the DAL, and select DAL -- Alt + P + R -- Solution -- project -- select MOdel -- and OK.

(3) Add a reference to system. configuration in DAL, and select DAL --> Alt + P + R --> assembly --> framework --> select System. configuration --> OK.

(4) create a data category and select DAL --> Shift + ALT + C --> Create a data category. UserDB class

1 namespace DAL 2 {3 class UserDB 4 {5 private string connString = ConfigurationManager. connectionStrings [connString]. toString (); 6 public int AddUser (UserInfo userInfo) 7 {8 // Add a user to the database. Operation 9 string commandText = insert into UserInfo (userName, Password) values (@ userName, @ Password); 10 SqlParameter [] paras = new SqlParameter [] 11 {12 new SqlParameter (@ userName, userInfo. userName), 13 new SqlParameter (@ Password, userInfo. password) 14}; 15 return SqlHelper. executeNonQuery (connString, CommandType. text, commandText, paras); 16} 17}

3. the business logic layer (BLL) performs logical judgment and discount on transmitted data and transfers the correct value.

 

 

(1) first create the business logic layer class library BLL, open the project, right-click the solution, and choose add> new project> select class library> rename BLL> OK.

(2) Add a reference to Model and DAL to BLL, and select BLL --> Alt + P + R --> solution --> project --> select MOdel and DAL --> OK.

(3) create a business logic class, and select BLL --> Shift + ALT + C --> create a business logic class. LoginManager class

1 namespace BLL 2 {3 public class LoginManager 4 {5 private UserDB userDB = new UserDB (); 6 public bool Add (UserInfo userInfo, out string messageStr) 7 {8 messageStr =; // return interface layer add user return information 9 bool isSuccess = false; 10 if (userInfo. userName. trim (). length! = 0) // determine whether the passed username is null 11 {12 if (userDB. isEquals (userInfo) // pass it to The DALl operation to determine whether there are repeated values in the database 13 {14 userDB. addUser (userInfo); // Add a new user 15 isSuccess = true; 16} 17 else18 messageStr = with the same value to the DAL operation; 19} 20 else21 {22 messageStr = cannot be blank; 23 24} 25 return isSuccess; // whether the return interface layer is successfully added 26} 27} 28}

4.The presentation layer (UI) is the user interface layer.

 

(1) Add a reference to Model and BLL in the UI. Select UI -- Alt + P + R -- Solution -- project -- and select MOdel and BLL --.

(2) write code to pass data to the BLL layer.

1 UserInfo userInfo; 2 LoginManager lm = new LoginManager (); 3 private void btnAdd_Click (object sender, EventArgs e) 4 {5 userInfo = new UserInfo () 6 {7 UserName = txtUserName. text. trim (), 8 Password = txtPassword. text. trim () 9}; 10 string messageStr =; 11 12 if (lm. add (userInfo, out messageStr) 13 {14 MessageBox. show (added successfully); 15} 16 else17 {18 MessageBox. show (messageStr); 19 txtUserName. focus (); 20} 21 22} 23}

 

Let's talk about how to display the information of the three tables as shown in one control.

To implement the content, you must first obtain the Student name, Subject name, test score, and test time from the Student, Subject, and StudentResult.

First, add an extension class. Here inheritance is used.

namespace Combox.Model{   public class StudentExtens:Student    {        public string SubjectName { get; set; }        public int StudentResult { get; set; }        public DateTime ExamDate { get; set; }    }}

Continue DAL Layer

 

1 public List <StudentExtens> SelectStudentResult () 2 {3 // view student scores 4 List <StudentExtens> list = new List <StudentExtens> (); 5 SqlConnection con = new SqlConnection ("Server = 192.168.15.21; initial catalog = MySchool; uid = sa;"); 6 DataTable dt = SQLHelper. executeDataTable (@ "select studentname, subjectname, studentresult, examdate from student, subject, result where student. studentno = result. studentno and result. subjectid = subject. subjectid "); 7 foreach (DataRow item in dt. rows) 8 {9 StudentExtens se = new StudentExtens (); 10 se. studentName = item ["studentname"]. toString (); 11 se. subjectName = item ["subjectname"]. toString (); 12 se. studentResult = Convert. toInt32 (item ["studentresult"]); 13 se. examDate = Convert. toDateTime (item ["examdate"]); 14 list. add (se); 15} 16 return list; 17}

Next is the BLL Layer

 1 namespace Combox.BLL 2 { 3    public class StudentBLL 4     { 5        StudentDAL sd = new StudentDAL(); 6        public List<StudentExtens> SelectStudentResult() 7        { 8           return sd.SelectStudentResult(); 9        }10     }11 }

Call in UI

1            StudentBLL sb = new StudentBLL();2            List<StudentExtens> list = sb.SelectStudentResult();3            dgvlist.DataSource = list;

 

 

Dear friends! Here the layers come to an end. Thank you for following me, paying attention to me, and paying attention to my friends. Come on!

 We can only:

Wolf eats meat! The combat capability of a team in the company determines how much meat you divide.

Ten or twenty years later, you will think about you now. As for you at that time, would you like to thank you for sitting in front of the computer screen? The answer is in every day!

Love coding love life love music love text love small M

The last day of March 16

Related Article

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.