三层登录C#实现
三層登錄C#實現
? ? ? ? ? 首先,添加項目建立三層,其中顯示層為Windows窗體應用程序(基于CS開發)或者是ASP.NET窗體應用程序(基于BS開發),業務邏輯層和數據訪問層都是類庫,不需要與用戶進行交互的界面。
? ? ? ? ??調用關系,顯示層調用業務邏輯層,業務邏輯層調用數據訪問層,而這三層都必須的調用實體層,調用關系就是引用的關系,首先添加引用然后才可以調用。
顯示層代碼:
namespace LoginUI {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void btnLogin_Click(object sender, EventArgs e){//傳統的數據庫連接//IDbConnection conn = new SqlConnection("c……");//IDbCommand cmd = conn.CreateCommand();//cmd.CommandText = "Select UserName From USERS WHERE ……";//cmd.ExecuteReader();string userName = txtUserName.Text.Trim();string password = txtPassWord.Text;Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();Login.Model.UserInfo user=mgr.UserLogin(userName, password);MessageBox.Show("登錄用戶:" + user.UserName);}} }業務邏輯層代碼:
namespace Login.BLL {public class LoginManager{public Login.Model.UserInfo UserLogin(string userName, string password){throw new NotImplementedException();Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();Login.Model.UserInfo user = uDao.SelectUser(userName, password);if (user != null)// login successfully{Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();sDao.UpdateScore(userName, 10);return user;}else{throw new Exception ("登錄失敗。");}}} }數據訪問層代碼:
namespace Login.DAL {class DbUtil{public static string ConnString = @"Server=vangjibin;Database=Login;User ID=sa;Password=vang_ji_bin";} } namespace Login.DAL {public class ScoreDAO{public void UpdateScore(string userName, int value){using (SqlConnection conn = new SqlConnection(DbUtil.ConnString)){SqlCommand cmd = conn.CreateCommand();cmd.CommandText = @"INSERT INTO SCORES(UserName,Score)Values(@UserName,@Score)";cmd.Parameters.Add(new SqlParameter("@UserName", userName));cmd.Parameters.Add(new SqlParameter("@Score", value));conn.Open();cmd.ExecuteNonQuery();}}} } namespace Login.DAL {public class UserDAO{public Login.Model.UserInfo SelectUser(string userName, string password){using (SqlConnection conn=new SqlConnection (DbUtil.ConnString)){SqlCommand cmd = conn.CreateCommand();cmd.CommandText =@"SELECT ID,UserName,Password,EmailFROM 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();Login .Model .UserInfo user=null ;while (reader.Read ()){if (user ==null ){user =new Login .Model .UserInfo ();}user .ID =reader .GetInt32 (0);user.UserName =reader .GetString (1);user .Password =reader .GetString (2); //not suggestionif (!reader.IsDBNull (3)){user.Email =reader .GetString (3);}}return user;}}} }實體層代碼:
<span style="font-size:24px;">namespace Login.Model {public class UserInfo{//Model為了在三層之間傳輸數據//在三個層中都要添加Model的引用public int ID { get; set; }public string UserName { get; set; }public string Password { get; set; }public string Email { get; set; }} } </span>《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
- 上一篇: 工厂三姐妹
- 下一篇: c# char unsigned_dll