登录窗口代码:- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.OleDb;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace 学生信息管理系统
- {
- public partial class FormLogin : Form
- {
- public FormLogin()
- {
- InitializeComponent();
- }
- #region 登录事件
- int errorTimes = 3;
- public static string userName, quanxian;
- private void btnLogin_Click(object sender, EventArgs e)
- {
- //判断用户名密码是否为空!
- if (txtName.Text == "" || txtPassword.Text == "")
- {
- MessageBox.Show("用户名或密码不能为空,请输入密码和用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- txtName.Focus();
- return;
- //Console.WriteLine("用户名或密码不能为空");
- }
- else
- {
- switch (DBClass.Login(txtName.Text, txtPassword.Text))
- {
- case 0://登录失败,用户名或密码错误
- errorTimes--;
- if (errorTimes>0)
- {
- MessageBox.Show("用户名或密码错误,你还能尝试" + errorTimes + "次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- txtName.Text = "";
- txtPassword.Text = "";
- txtName.Focus();
- }
- else
- {
- MessageBox.Show("错误次数过多,程序退出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- Application.Exit();
- return;
- }
- break;
- case 1://登录成功并确定权限为0;
- userName = txtName.Text;
- quanxian = "管理员";
- MessageBox.Show("欢迎您" + txtName.Text, "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- MdiFather mdi = new MdiFather();
- mdi.Show();
- this.Hide();
- break;
- case 2://登录成功并确定权限为1;
- userName = txtName.Text;
- quanxian = "学生";
- MessageBox.Show("欢迎您" + txtName.Text, "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- mdi = new MdiFather();
- mdi.Show();
- this.Hide();
- break;
- default://用不到的,万一出现情况程序重启!
- //throw Exception ex;
- MessageBox.Show("程序出错\n即将退出", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- Application.Restart();
- break;
- }
- }
- //string sql = string.Format("select*from userID where 用户名='{0}' and 密码='{1}'", txtName.Text, txtPassword.Text);
- ////判断用户名密码是否为空!
- //if (txtName.Text == "" || txtPassword.Text == "")
- //{
- // MessageBox.Show("用户名或密码不能为空,请输入密码和用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- // txtName.Focus();
- // //Console.WriteLine("用户名或密码不能为空");
- //}
- //else if (DBClass.Login(sql))
- //{
- // MessageBox.Show("欢迎您" + txtName.Text, "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- // MdiFather mdi = new MdiFather();
- // mdi.Show();
- // this.Hide();
- // DBClass.conn.Close();
- //}
- //else
- //{
- // MessageBox.Show("用户名或密码错误,请输入正确的密码和用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- // txtName.Text = "";
- // txtPassword.Text = "";
- // txtName.Focus();
- //}
- }
- private void btnReset_Click(object sender, EventArgs e)
- {
- txtName.Text = "";
- txtPassword.Text = "";
- txtName.Focus();
- }
- #endregion
- }
- }
复制代码 |