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
- private void button1_click (object sender, EventArgs e)
- {
- //Instantiate a model object to pass information to the D layer
- Login.Model.UserInfo user = new Login.Model.UserInfo ();
- //Instantiate a model object to receive information from the D layer
- Login.Model.UserInfo User = new Login.Model.UserInfo ();
- //argument passed to the object
- User. UserName = TxtUserName.Text.Trim ();
- //Instantiate layer B
- UserQuery.BLL.QueryBLL Userq = new UserQuery.BLL.QueryBLL ();
- Try
- {
- //Pass the object to Layer B processing
- 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.P Assword + " \ r \ n" + "user Email:" + user.email;
- }
- }
2. The BLL layer
[CSharp]View Plaincopy
- Public class QUERYBLL
- {
- Public Login.Model.UserInfo userquery (Login.Model.UserInfo user)
- {
- //Instantiate D layer
- UserQuery.DAL.QueryDAL userqd = new UserQuery.DAL.QueryDAL ();
- //Instantiate users
- Login.Model.UserInfo User = new Login.Model.UserInfo ();
- Try
- {
- //Let D layer determine whether the information is correct
- user = Userqd.querydao (user);
- }
- catch (Exception ex)
- {
- throw ex; //Throw the error message to the U-layer
- }
- //return user information
- return User;
- }
- }
3. DAL layer
[CSharp]View Plaincopy
- Public Login.Model.UserInfo Querydao (Login.Model.UserInfo user)
- {
- using (SqlConnection conn=new SqlConnection (dbutil.connstring))
- {
- SqlCommand cmd = conn. CreateCommand (); //For working with databases
- //Determine if the user name exists
- Cmd.commandtext = @"SELECT * from USERS WHERE [email protected]";
- //Get Query Language
- Cmd.commandtype = CommandType.Text;
- //Add parameters in query Language
- Cmd. Parameters.Add (new SqlParameter ("@UserName", user. UserName));
- Conn. Open ();
- //Query and return results
- SqlDataReader reader = cmd. ExecuteReader ();
- //Declare a return instance
- Login.Model.UserInfo User = new Login.Model.UserInfo ();
- Try
- {
- //If the user name exists, the user information is returned
- if (reader. Read ())
- {
- //Get user information
- User.ID = reader. GetInt32 (0);
- User.username = reader. GetString (1);
- User.password = reader. GetString (2);
- //If the mailbox is not empty, remove
- if (!reader. IsDBNull (3))
- {
- User.email = reader. GetString (3);
- }
- }
- Else
- {
- //If not present, throw an error
- throw New Exception ("The user does not exist! ");
- }
- //Disconnect
- Conn. Close ();
- }
- catch (Exception ex)
- {
- throw ex; //Throw the error message to D-Layer processing
- }
- return User; //Return user information
- }
- }
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