Three-storey architecture and four Heavenly Kings--a search

Source: Internet
Author: User

Last time we talked about how to use the three-tier architecture to remove data from a database, this time we'll talk about how to find data in a database, but in the last article it also involves finding, but that's showing all the records in the entire table, and we're going to look for one of the records we need this time.

1. UI Layer

The first is the interface, remember the first time we added the "Faye Wong" that record, today we will find it out

[CSharp]View Plaincopy
  1. private void button1_click (object sender, EventArgs e)
  2. {
  3. //Instantiate a model object to pass information to the D layer
  4. Login.Model.UserInfo user = new Login.Model.UserInfo ();
  5. //Instantiate a model object to receive information from the D layer
  6. Login.Model.UserInfo User = new Login.Model.UserInfo ();
  7. //argument passed to the object
  8. User. UserName = TxtUserName.Text.Trim ();
  9. //Instantiate layer B
  10. UserQuery.BLL.QueryBLL Userq = new UserQuery.BLL.QueryBLL ();
  11. Try
  12. {
  13. //Pass the object to Layer B processing
  14. user = Userq.userquery (user);
  15. }
  16. catch (Exception ex)
  17. {
  18. MessageBox.Show (ex. Message);
  19. return;
  20. }
  21. Txtinformation.text = "user ID:" + user.id + "\ r \ n" + "user name:" + user.username + "\ r \ n" + " User password:" + USER.P  Assword + " \ r \ n" + "user Email:" + user.email;
  22. }
  23. }



2. The BLL layer [CSharp]View Plaincopy
  1. Public class QUERYBLL
  2. {
  3. Public Login.Model.UserInfo userquery (Login.Model.UserInfo user)
  4. {
  5. //Instantiate D layer
  6. UserQuery.DAL.QueryDAL userqd = new UserQuery.DAL.QueryDAL ();
  7. //Instantiate users
  8. Login.Model.UserInfo User = new Login.Model.UserInfo ();
  9. Try
  10. {
  11. //Let D layer determine whether the information is correct
  12. user = Userqd.querydao (user);
  13. }
  14. catch (Exception ex)
  15. {
  16. throw ex; //Throw the error message to the U-layer
  17. }
  18. //return user information
  19. return User;
  20. }
  21. }
3. DAL layer

[CSharp]View Plaincopy
  1. Public Login.Model.UserInfo Querydao (Login.Model.UserInfo user)
  2. {
  3. using (SqlConnection conn=new SqlConnection (dbutil.connstring))
  4. {
  5. SqlCommand cmd = conn.      CreateCommand (); //For working with databases
  6. //Determine if the user name exists
  7. Cmd.commandtext = @"SELECT * from USERS WHERE [email protected]";
  8. //Get Query Language
  9. Cmd.commandtype = CommandType.Text;
  10. //Add parameters in query Language
  11. Cmd. Parameters.Add (new SqlParameter ("@UserName", user.  UserName));
  12. Conn. Open ();
  13. //Query and return results
  14. SqlDataReader reader = cmd. ExecuteReader ();
  15. //Declare a return instance
  16. Login.Model.UserInfo User = new Login.Model.UserInfo ();
  17. Try
  18. {
  19. //If the user name exists, the user information is returned
  20. if (reader. Read ())
  21. {
  22. //Get user information
  23. User.ID = reader. GetInt32 (0);
  24. User.username = reader. GetString (1);
  25. User.password = reader. GetString (2);
  26. //If the mailbox is not empty, remove
  27. if (!reader. IsDBNull (3))
  28. {
  29. User.email = reader. GetString (3);
  30. }
  31. }
  32. Else
  33. {
  34. //If not present, throw an error
  35. throw New Exception ("The user does not exist!    ");
  36. }
  37. //Disconnect
  38. Conn. Close ();
  39. }
  40. catch (Exception ex)
  41. {
  42. throw ex; //Throw the error message to D-Layer processing
  43. }
  44. return User; //Return user information
  45. }
  46. }
4. Results5. Summary

This simple query process with the letter, first we (U layer) to write the letter into the envelope (the user name into the entity), and then handed over to the postman (layer B) processing, and finally to the recipient (d) hands, after the recipient's processing, the reply (to return the information) into the envelope (return to the entity), And then sent to us by the postman, so that we can according to the reply, to make the corresponding treatment. Of course, if anything goes wrong in this process, the postman will also tell us that, like this, we can communicate conveniently through the postman, and through our cooperation we will be able to organize a complex work in an orderly way. Therefore, I believe that as long as we all work together, the world must become better.


Three-storey architecture and four Heavenly Kings--a search

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.