Three-tier architecture and four kings -- Query

Source: Internet
Author: User

The last time we talked about how to use a three-tier architecture to delete data in the database, this time we will talk about how to find data in the database. In fact, this article also involves searching, but it shows all the records in the table, and this time we will look for a record we need.

1. UI Layer

The first is the interface. Do you still remember the record "Faye Wong" We added for the first time? Today we will find it.

Private void button#click (object sender, EventArgs e) {// instantiate a model object to pass information Login to layer D. model. userInfo user = new Login. model. userInfo (); // instantiate a model object to accept information Login from layer D. model. userInfo User = new Login. model. userInfo (); // pass the parameter to the user of the object. userName = txtUserName. text. trim (); // instantiate UserQuery at Layer B. BLL. queryBLL userQ = new UserQuery. BLL. queryBLL (); try {// pass the object to layer B to process User = userQ. userQuery (user);} catch (Exception ex) {MessageBox. show (ex. message); return;} txtInformation. text = "User ID:" + User. ID + "\ r \ n" + "User name:" + User. userName + "\ r \ n" + "User Password:" + User. password + "\ r \ n" + "User Email:" + User. email ;}}


2. BLL Layer

Public class QueryBLL {public Login. model. userInfo UserQuery (Login. model. userInfo user) {// instantiate layer D UserQuery. DAL. queryDAL UserQD = new UserQuery. DAL. queryDAL (); // instantiate the user Login. model. userInfo User = new Login. model. userInfo (); try {// Let layer D determine whether the information is correct. User = UserQD. queryDao (user) ;}catch (Exception ex) {throw ex; // throw the error message to the U layer} // return the User information return user ;}}

3. DAL Layer

Public Login. model. userInfo QueryDao (Login. model. userInfo user) {using (SqlConnection conn = new SqlConnection (DBUtil. connString) {SqlCommand cmd = conn. createCommand (); // used to process the database // determine whether the user name exists cmd. commandText = @ "SELECT * from users where UserName = @ UserName"; // obtain the query language cmd. commandType = CommandType. text; // Add the cmd parameter in the query language. parameters. add (new SqlParameter ("@ UserName", user. userName); conn. open (); // query The returned result is SqlDataReader reader = cmd. executeReader (); // declare a returned instance Login. model. userInfo User = new Login. model. userInfo (); try {// if the user name exists, the user information if (reader. read () {// obtain User information User. ID = reader. getInt32 (0); User. userName = reader. getString (1); User. password = reader. getString (2); // if the mailbox is not empty, retrieve if (! Reader. IsDBNull (3) {User. Email = reader. GetString (3) ;}} else {// if not, throw the throw new Exception ("this User does not exist! ");} // Disconnect conn. close () ;} catch (Exception ex) {throw ex; // throw the error message to layer D for processing} return User; // return User information }}
4. Results

5. Summary

This simple query process is similar to sending a mail. First, we (U layer) should load the written mail into an envelope (that is, the user name is passed into the entity), and then hand it to the postman (B layer) for processing, finally, the receiver (layer D) loads the reply (the information to be returned) into the envelope (the entity to be returned) and then sends it to us by the postman, in this way, we can handle the response accordingly. Of course, if there are any errors in this process, the postman will also inform us that, in this way, we can achieve convenient communication through the postman, through our cooperation, we can make a complicated job orderly. Therefore, I believe that as long as we all work together, the world will surely become a better place.






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.