C # Three-tier logon code tutorial,

Source: Internet
Author: User

C # Three-tier logon code tutorial,
[C #] layer-3 login Architecture

The U layer calls layer B and layer B calls layer D.

Code U Layer
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace UI {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void initialize login_click (object sender, EventArgs e) {// The display layer cannot deal with the data source. try {string userName = txtUserName. text. trim (); string password = txtPassword. text; BLL. BLL. administrator admin = new BLL. BLL. administrator (); Model. model. userInfo user = admin. userLogin (userName, password); MessageBox. show ("Login user:" + user. userName);} catch (Exception ex) {MessageBox. show (ex. message );}}}}
Layer B
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data. sqlClient; namespace BLL. BLL {public class Administrator {public Model. model. userInfo UserLogin (string userName, string password) {DAL. DAL. userDAO uDao = new DAL. DAL. userDAO (); Model. model. userInfo user = uDao. selectUser (userName, password); if (user! = Null) {DAL. DAL. scoreDAO sDao = new DAL. DAL. scoreDAO (); sDao. updateScore (user. ID, userName, 10); return user;} else {throw new Exception ("Logon Failed. ");}}}}
Layer D
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; // using System. data; // using System. data. sqlClient; namespace DAL. DAL {class DbUtil {public static string ConnString = @ "Server = .; database = Login; User ID = sa; Password = 123456 ";}} using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data. sqlClient; namespace DAL. DAL {public class ScoreDAO // Add 10 points {public void UpdateScore (int ID, string userName, int value) {using (SqlConnection conn = new SqlConnection (DbUtil. connString) {SqlCommand cmd = conn. createCommand (); // create a command object cmd. commandText = @ "insert into scores (ID, UserName, Score) Values (@ ID, @ UserName, @ Score)"; // modify the Score table data cmd. parameters. add (new SqlParameter ("@ ID", ID); cmd. parameters. add (new SqlParameter ("@ userName", userName); cmd. parameters. add (new SqlParameter ("@ Score", value); conn. open (); cmd. executeNonQuery () ;}}} using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data. sqlClient; using System. data; namespace DAL. DAL {public class UserDAO {public Model. model. userInfo SelectUser (string userName, string password) {using (SqlConnection conn = new SqlConnection (DbUtil. connString) {SqlCommand cmd = conn. createCommand (); cmd. commandText = @ "select id, UserName, Password, Email from users where UserName = @ UserName AND Password = @ Password"; cmd. commandType = CommandType. text; cmd. parameters. add (new SqlParameter ("@ UserName", userName); cmd. parameters. add (new SqlParameter ("@ Password", password); conn. open (); SqlDataReader reader = cmd. executeReader (); Model. model. userInfo user = null; while (reader. read () {if (user = null) // if not, generate a new {user = new Model. model. userInfo ();} user. ID = reader. getInt32 (0); user. userName = reader. getString (1); user. password = reader. getString (2); user. email = reader. getString (3) ;}return user ;}}}}
Model Layer
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; // transmits data between three layers without knowing the other layers. The three independent assemblies do not reference the third-layer namespace Model. model {// data is typically encapsulated. In the business logic layer, public class UserInfo {public int ID {get; set;} public string UserName {get; set;} public string Password {get; set ;}public string Email {get; set ;}}}
Tips

1. the username, password, and other information should be inserted in the user table as the original administrator.

2. After successfully logging on, log on again to delete the inserted score table data

3. regenerate the solution

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.