WinForm联合MySQL实现注册登录系统

任何一个应用首先具备注册登录功能。

~~~~进入正题~~~~

一、创建一个注册窗体,如下图:、

WinForm联合MySQL实现注册登录系统_第1张图片

using System;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace 登录注册系统
{
    public partial class Frm_Reg : Form
    {
        public Frm_Reg()
        {
            InitializeComponent();
        }

        private void Frm_Reg_Load(object sender, EventArgs e)
        {

        }
        /// 
        /// 账号长度判断
        /// 
        /// 
        /// 
        private void text_Accout_TextChanged(object sender, EventArgs e)
        {
            if (text_Accout.Text.Length > 8)
            {
                MessageBox.Show("账号长度最大为8个字符!","提示:",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                text_Accout.Text = text_Accout.Text.Substring(0, 8);
                return;
            }
        }
        /// 
        /// 注册
        /// 
        /// 
        /// 
        private void btn_Reg_Click(object sender, EventArgs e)
        {
            InputConfirm();
            Frm_Login frm_Login = new Frm_Login();
            frm_Login.ShowDialog();
        }
        /// 
        /// 输入判断
        /// 
        private void InputConfirm()
        {
            string account = text_Accout.Text.Trim(); // 去除输入的字符串两端空格
            account = text_Accout.Text.Replace(" ", ""); // 去除字符串中间的空格
            string pwd = text_Pwd.Text.Trim();
            pwd = text_Pwd.Text.Replace(" ", "");
            string confirm = text_Confirm.Text.Trim();
            confirm = text_Confirm.Text.Replace(" ", "");
            string qq = text_qq.Text.Trim();
            qq = text_qq.Text.Replace(" ", "");
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(confirm) || string.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("请输入前三项!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pwd != confirm)
            {
                MessageBox.Show("两次输入密码不一致!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                text_Pwd.Text = "";
                text_Confirm.Text = "";
                return;
            }
            OperateSQL();
        }

        /// 
        /// 数据库的操作
        /// 
        private void OperateSQL()
        {
            MySqlConnection conn;
            try
            {
                string sqlstr = "server = localhost; uid = root; pwd = 123456;database = Carhome";
                conn = new MySqlConnection(sqlstr);
                conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return;
            }
            string str = String.Format("INSERT INTO carusertable(`UserName`, `Pws`, `QQ`) VALUES ('{0}', '{1}', '{2}');",
                text_Accout.Text, text_Pwd.Text, text_Confirm.Text, text_qq.Text);
            MySqlCommand comm = new MySqlCommand(str, conn);
            int res = comm.ExecuteNonQuery();
            if (res > 0)
            {
                conn.Close();
                conn.Dispose();
                DialogResult dr = MessageBox.Show("用户[" + text_Accout.Text + "]注册成功!","", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    Frm_Reg frm_Reg = new Frm_Reg();
                    frm_Reg.Close();
                    //this.Dispose();
                }
            }
            else
            {
                MessageBox.Show("注册失败!请重新注册...");
                conn.Close();
                conn.Dispose();
                return;
            }
        }
    }

}
 
注册成功后如下图所示:

WinForm联合MySQL实现注册登录系统_第2张图片


注册成功后,就可以跳转到登录,登录是有登陆验证的。验证账号或者密码是否正确,通过查询数据库操作实现。代码如下:

    private void btn_Login_Click(object sender, EventArgs e)
        {
            try
            {
                string sqlstr = "server = localhost; uid = root; pwd = 123456; database = Carhome";
                MySqlConnection conn = new MySqlConnection(sqlstr);
                conn.Open();
                string UserNamestr = string.Format("SELECT * FROM carusertable where UserName = '{0}' and Pws = {1};",
                    text_Account.Text,text_Pwd.Text);
                MySqlCommand comm = new MySqlCommand(UserNamestr, conn);
                MySqlDataReader dr = comm.ExecuteReader();
                if (dr.Read())
                {
                    string name = dr.GetString(dr.GetOrdinal("UserName"));
                    string pwd = dr.GetString(dr.GetOrdinal("Pws"));
                    if (name == text_Account.Text && pwd == text_Pwd.Text)
                    {
                        MessageBox.Show("登录成功");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码错误,请检查后输入!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
        }

运行如下:

用户名或者密码错误进行提示,然后再次输入进行验证:

WinForm联合MySQL实现注册登录系统_第3张图片

当验证成功后,进行登录提示:

WinForm联合MySQL实现注册登录系统_第4张图片

··························································································优美分割线···································································································

在用户进行注册的时候应该也有验证,验证用户输入的数据是否存在,只要是用户名是否重复;用户输入是否合法(正则表达式),或者用户权限选择等等。

至此一个建议的 winform 注册登录系统就已经实现!





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