C#图书管理系统之用户注册(二)

 

 

 

 

窗体设计:

C#图书管理系统之用户注册(二)_第1张图片

通过dao层连接数据库,向数据库中插入用户信息,判断用户是否已经被注册

 


//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()
        {

        }
        //判断用户是否已经被注册
        /// 
        /// 
        /// 
        /// 
        /// 如果flag==true,表明用户已经注册
        public Boolean sameUser(User user)
        {
            bool flag = false;    //判断标志
            SqlConnection sqlCon = null;
            sqlCon = DbUtil.getConnection();
            sqlCon.Open();
            string sql = @"SELECT count (*) FROM [user] WHERE userName='" + user.UserName + "'";
            SqlCommand sqlCommand = new SqlCommand(sql, sqlCon);
            int count = Convert.ToInt32(sqlCommand.ExecuteScalar());
            if (count > 0)
            {
                flag = true;    //表明用户已经注册
            }
            else
            {
                flag = false;
            }

            return flag;
        }
        //用户注册
        /// 
        /// 
        /// 
        /// 
        /// 值为1表明注册成功
        public int  Register(User user)
        {
            int query;      
       
            SqlConnection sqlCon = null;
            sqlCon = DbUtil.getConnection();
            sqlCon.Open();
            string sql = @"INSERT INTO [user](userName,password,email,phoneNumber)
                        " + " VALUES('" + user.UserName + "','" + user.Password + "','" + user.Email + "','" + user.PhoneNumber + "')";
            SqlCommand sqlCommand = new SqlCommand(sql, sqlCon);
            query=sqlCommand.ExecuteNonQuery();
           
            sqlCon.Close();
            return query;
            
  
        }
        //用户登陆验证
        /// 
        /// 
        /// 
        /// 
        /// 
        /// flag==true,表明登陆成功
        public Boolean userLogin(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)
            {
                throw ex;
            }
            finally
            {
            sqlCon.Close();

            }
            return flag;
        }
    }
}

窗体代码实现注册按钮以及重置按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util;
using Dao;
using Model;
using System.Text.RegularExpressions;


namespace WindowsFormsApp1
{
    public partial class RegisterForm : Form
        
    {
        UserDao userDao = new UserDao();
      
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

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

        

       

        //用户注册按钮事件触发
        /// 
        /// 
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            
            //获取文本框中的值
            string userName = userNameTxt.Text;
            string password = passwordTxt.Text;
            string checkPassword = checkpasswordTxt.Text;
            string email = emailTxt.Text;
            string phoneNumber = phoneNumberTxt.Text;
            if (stringUtil.isEmpty(userName))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            if (stringUtil.isEmpty(password)&& stringUtil.isEmpty(checkPassword))
            {
                MessageBox.Show("密码不能为空");
                return;
            }
            if (stringUtil.isEmpty(email))
            {
                MessageBox.Show("邮箱不能为空");
                return;
            }
            //利用正则表达式判断邮箱是否符合格式
            if(Regex.IsMatch(email, @"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?") == false)
            {
                MessageBox.Show("请输入正确的邮箱格式");
                return;
            }
            if (stringUtil.isEmpty(phoneNumber))
            {
                MessageBox.Show("手机号不能为空");
                return;
            }
            //利用正则表达式判断手机号码是否符合格式
            if (Regex.IsMatch(phoneNumber, "[0-9]{11,11}") ==false)
            if (password != checkPassword)
            {
                MessageBox.Show("密码不一致,请重新输入");
            }
            User user = new User(userName,password,email,phoneNumber);//将文本框中的数据传入用户对象
            int x = userDao.Register(user);         //返回查询值
            if (x == 1&&userDao.sameUser(user)==false)  //判断用户是否存在以及注册事件
            {
                MessageBox.Show("注册成功");
                LoginForm loginForm = new LoginForm();
                loginForm.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("用户已存在,注册失败");
            }
        }
        //重置事件触发
        private void button2_Click(object sender, EventArgs e)
        {
            userNameTxt.Text = "";
            passwordTxt.Text = "";
            checkpasswordTxt.Text = "";
            emailTxt.Text = "";
            phoneNumberTxt.Text = "";
        }
    }
}

 

你可能感兴趣的:(C#图书管理系统之用户注册(二))