C#图书管理系统之用户登陆界面实现(一)

使用MVC思想,大题目录结构如下

C#图书管理系统之用户登陆界面实现(一)_第1张图片

设计表

C#图书管理系统之用户登陆界面实现(一)_第2张图片

数据库连接工具类

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Util
{
    public class DbUtil
    {
        //数据库连接
        public static SqlConnection getConnection()
        {
        string strCon = @"Data Source=.; Initial Catalog=TestDB; Integrated Security=True;";
        SqlConnection sqlCon = new SqlConnection(strCon);
            
            return sqlCon;
        }
       
    }
}

用户类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    //用户类
    public class User
    {
        private int id;                 //用户编号
        private string userName;        //用户名
        private string password;        //用户密码
        private string email;           //用户邮箱
        private string phoneNumber;     //用户手机号

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string UserName
        {
            get { return userName; }
            set { userName = value; }

        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        public string PhoneNumber
        {
            get { return phoneNumber; }
            set { phoneNumber = value; }
            
        }


        public User()
        {

        }

        public User(string userName,string password)
        {
            this.userName = userName;
            this.password = password;
        }

        public User(int id,string userName, string password)
        {
            this.id = id;
            this.userName = userName;
            this.password = password;
        }

    }
}

通过Dao层连接数据库进行用户身份验证

注:这里sql语句会发生sql注入,极不安全


//using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Util;
using Model;
namespace Dao
{
    public class UserDao
    {
        
        public UserDao()
        {

        }
        public Boolean Login(string userName,string password)
        {
            bool flag=false;
            SqlConnection sqlCon;
            sqlCon = DbUtil.getConnection();
            sqlCon.Open();
            try
            {
           // string sql= "SELECT userName,password FROM [user] where userName='"+userName+"'AND password ='"+password+"'";
                string sql = "SELECT * FROM [user] WHERE username='" + userName + "' AND password='" + password + "'";
                SqlCommand command = new SqlCommand(sql,sqlCon);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                flag = true;
            }
            else
            {
                flag= false;
            }
            }
            catch(Exception ex)
            {
            }
            sqlCon.Close();
            return flag;
        }
    }
}

 

窗口登陆点击事件

using Model;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util;
using Dao;
namespace WindowsFormsApp1
{
    public partial class LoginForm : Form
    {
        

        public LoginForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取文本框的内容
            string userName = userNameTxt.Text;
            string password = passwordTxt.Text;
            bool flag;
            if (stringUtil.isEmpty(userName))
            {
                MessageBox.Show("用户名不能为空!");
                return;
            }
            if (stringUtil.isEmpty(password))
            {
                MessageBox.Show("密码不能为空!");
                return;
            }
            //实例化用户
            User user = new User(userName, password);


            if (stringUtil.isNotEmpty(userName) && stringUtil.isNotEmpty(password))
            {
                UserDao userDao = new UserDao();
                //将用户的用户名和密码传入
                flag = userDao.Login(user.UserName, user.Password);

                if (flag)
                {
                    MessageBox.Show("登陆成功!!!");
                }
                else
                {
                    MessageBox.Show("登陆失败!!!");
                }
            }

        }
        //重置文本框
        private void button2_Click(object sender, EventArgs e)
        {
            userNameTxt.Text = "";
            passwordTxt.Text = "";

        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
             

            
        }
        //跳转注册界面
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
            Register rs=new Register();
            rs.Show();
            this.Hide();
            
        }
    }
}

效果如下:

C#图书管理系统之用户登陆界面实现(一)_第3张图片

最后,能熟练使用debug非常重要!!!

你可能感兴趣的:(C#)