首先确定一个事情,登陆界面不是进程的主页面。也就是说,主进程首先加载的不是登陆页面。

1:首页建立个主页面MainForm。再建立个登陆页面LoginFrom。各自页面name属性设置为与类相同的名字。

2: 在主页面的初始化中加入如下代码: 

1 public MainForm()
2         {
3             InitializeComponent();
4             LoginFrom lf = new LoginFrom();
5             lf.ShowDialog();
6             this.gbLogin.Text = "欢迎" + lf.userName;
7         }
View Code

3:设计登陆页面:

     1: 首页 窗体属性 ControlBox 设置为false。StartPosition 设置为CenterScreen。

     2: 拽托控件设置页面。有登陆名,密码,登陆按钮即可。其中密码控件的属性passwordchar 设置为*。

     3:添加登陆按钮点击事件。代码如下:   

 1         private void btnLogin_Click(object sender, EventArgs e)
 2         {
 3             if (string.IsNullOrEmpty(tbUserName.Text.Trim()) || string.IsNullOrEmpty(tbPassword.Text.Trim()))
 4             {
 5                 MessageBox.Show("账户密码不能为空!");
 6             }
 7             else
 8             {
 9                 BLL.UserBLL userbll = new BLL.UserBLL();
10                 if (userbll.UserLogin(tbUserName.Text.Trim(), tbPassword.Text.Trim()))
11                 {
12                     userName = tbUserName.Text.Trim();
13                     this.Close();
14                 }
15                 else
16                 {
17                     MessageBox.Show("登陆失败!账户密码错误!");
18                 }
19             }
20         }
View Code

 其中BLL为业务逻辑层。主要操作数据库链接。

4:登陆成功后,要在主页面显示登陆的用户名。这就牵扯到了跨窗体数据传输。这里使用最简单的,在登陆窗体定义

      public string userName;然后主船体访问。

5: BLL代码如下:

       

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace BLL
 8 {
 9     public class UserBLL
10     {
11         private EFSql.SqlDbContext _context;
12         public UserBLL()
13         {
14             if (_context == null)
15             {
16                 _context = new EFSql.SqlDbContext();
17             }
18         }
19 
20         public bool UserLogin(string name, string password)
21         {
22             Model.UserInfo user = _context.UserInfos.Where(p => p.userName == name && p.userPassword == password).FirstOrDefault();
23             if (user != null)
24             {
25                 user.lastLoginTime = DateTime.Now;
26                 _context.SaveChanges();
27                 return true;
28             }
29             else
30             {
31                 return false;
32             }
33         }
34 
35         public bool AddUser(string name, string password)
36         {
37             Model.UserInfo user = new Model.UserInfo();
38             user.userCode = Guid.NewGuid().ToString().Replace("-","");
39             user.userName = name;
40             user.userPassword = password;
41             _context.UserInfos.Add(user);
42             _context.SaveChanges();
43             return true;
44         }
45 
46         public List<Model.UserInfo> GetUserList(string pwd)
47         {
48             return _context.UserInfos.AsNoTracking().ToList();
49         }
50     }
51 }
View Code

 

 posted on 2018-01-31 11:30  一朵茉莉花  阅读(3811)  评论(1编辑  收藏  举报