C#用户登录功能

csharp

浏览数:139

2019-1-7


登陆界面对登陆对象的选择及判断

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;  

namespace StudentManage.Login
{
    public partial class LoginInterfaceForm : Form
    {
        public LoginInterfaceForm()
        {
            InitializeComponent();
        }
        private void LoginInterfaceForm_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //连接数据库
            string str= @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\StudentManage\数据库文件\stumanage.mdf;Integrated Security=True;Connect Timeout=30";
            SqlConnection coon = new SqlConnection(str);
            coon.Open();

            string strErr = "";
            string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["StudentManager"].ConnectionString;
            Manage.Login.Login objLogin = new Manage.Login.Login(strConnection);

            if (rabAdmin.Checked == true)
            {
                Model.Users.Users objUsers = new Model.Users.Users();
                objUsers.UserNo= txtUserNo.Text.Replace("'", "''");
                objUsers.UserNo = objUsers.UserNo.Trim();
                
                objUsers.PassWord = txtPassWord.Text.Replace("'", "''");
                objUsers.PassWord = objUsers.PassWord.Trim();
                           
                if (txtUserNo.Text == "admin" && txtPassWord.Text == "123")
                {
                    objUsers.Rights = 1;
                    MainForm objEnter = new MainForm(objUsers.UserNo, objUsers.PassWord, objUsers.Rights);
                    this.Visible = false;
                    objEnter.Show();
                }
                else
                {
                    MessageBox.Show("您输入的账号或密码错误,请重新输入!");
                    txtUserNo.Text = "";
                    txtPassWord.Text = "";
                    return;
                }
            }
            else if (rabTea.Checked == true)  //教师登陆
            {
                Model.Users.Users objUsers = new Model.Users.Users();
                objUsers.UserNo = txtUserNo.Text.Replace("'", "''");
                objUsers.UserNo = objUsers.UserNo.Trim();
                objUsers.UserNo = objUsers.UserNo.Replace("%", @"\%");
               
                objUsers.PassWord = txtPassWord.Text.Replace("'", "''");
                objUsers.PassWord = objUsers.PassWord.Trim();
                objUsers.PassWord = objUsers.PassWord.Replace("%", @"\%");
                
                objUsers.Rights = 2;
                int iRent = objLogin.JudgeExist(objUsers.UserNo, objUsers.PassWord,objUsers.Rights, out strErr);
                if (iRent == 1)
                {
                    //objUsers.Rights = 2;
                    MainForm objEnter = new MainForm(objUsers.UserNo, objUsers.PassWord, objUsers.Rights);
                    objEnter.Show();
                    this.Visible = false;
                }
                else
                {
                    MessageBox.Show("您输入的账号或密码错误,请重新输入!");
                    txtUserNo.Text = "";
                    txtPassWord.Text = "";
                    return;
                }
            }
            else         //学生登陆
            {
                Model.Users.Users objUsers = new Model.Users.Users();
                objUsers.UserNo = txtUserNo.Text.Replace("'", "''");
                objUsers.UserNo = objUsers.UserNo.Trim();
               
                objUsers.PassWord = txtPassWord.Text.Replace("'", "''");
                objUsers.PassWord = objUsers.PassWord.Trim();
               
                objUsers.Rights = 3;
                int iRent = objLogin.JudgeExist(objUsers.UserNo, objUsers.PassWord, objUsers.Rights, out strErr);
                if (iRent == 1)
                {
                    //objUsers.Rights = 3;
                    MainForm objEnter = new MainForm(objUsers.UserNo, objUsers.PassWord, objUsers.Rights);
                    objEnter.Show();
                    this.Visible = false;
                }
                else
                {
                    MessageBox.Show("您输入的账号或密码错误,请重新输入!");
                    txtUserNo.Text = "";
                    txtPassWord.Text = "";
                    return;
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}