【C#】【MySQL】C#连接MySQL数据库(三)登陆注册代码

这是一个ASP.NET Web应用程序的登录和注册页面示例。代码包括`WebForm_Login.aspx`和`WebForm_Reg.aspx`,分别处理用户登录和注册功能。登录页面检查用户名和密码的合理性,并与数据库进行验证;注册页面验证用户名、密码和邀请码,然后将新用户信息写入数据库。所有操作都与MySQL数据库进行交互。
摘要由CSDN通过智能技术生成

项目结构

项目代码

WebForm_Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_Login.aspx.cs" Inherits="WebApplication_OmtpcMgrSystem.sign.WebForm_Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lbl1" runat="server" Text="用户名"></asp:Label>
            <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
        </div>
        <asp:Label ID="lbl2" runat="server" Text="密码"></asp:Label>
        <asp:TextBox ID="tb2" runat="server"></asp:TextBox>
        <br />
      <asp:Label ID="lbl_Message" runat="server" Text=""></asp:Label>
        <br />
        <asp:Button ID="btl_Login" runat="server" Text="登录" OnClick="btl_Login_Click" />
        <br />
        <asp:HyperLink ID="hre_forget" runat="server">忘记密码</asp:HyperLink>
        <asp:HyperLink ID="hre_reg" runat="server">注册</asp:HyperLink>
    </form>
</body>
</html>
WebForm_Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;


namespace WebApplication_OmtpcMgrSystem.sign
{
    public partial class WebForm_Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hre_reg.NavigateUrl = "WebForm_Reg.aspx";
        }

        protected void btl_Login_Click(object sender, EventArgs e)
        {
            //接受前端数据并进行简单处理
            string usrName = tb1.Text.Trim();
            string usrPwd = tb2.Text.Trim();
            //验证数据是否合理
            if (usrName.Length == 0 || usrName.Length > 100)
            {
                lbl_Message.Text = "UserName is wrong!";
            };
            if (usrPwd.Length < 6 || usrPwd.Length > 100)
            {
                lbl_Message.Text = "UserPassword is wrong!";
            }
            //try
            //{
                //设计连接字符串(连接数据库)
                string conn =
                    "Data Source = 127.0.0.1;" +
                    "User ID=root;" +
                    "Password=qq2686485465;" +
                    "DataBase=omtpc;" +
                    "port=3306";
                //定义连接对象(构造函数的参数为数据库连接字符串)
                MySqlConnection con = new MySqlConnection(conn);
                //打开数据库连接
                con.Open();
                //执行数据库的访问操作
                string strSqlCommand = "Select*from officer21 where usrID='" + usrName + "'";
            MySqlCommand cmd = new MySqlCommand(strSqlCommand, con);
                MySqlDataReader dr = cmd.ExecuteReader();//查找多行 : ExecuteReader()方法 | 执行结果放入dr中
                //dr.Read();//读出dr内容

            if (dr.Read())
            {
                string queryPassword = dr["password"].ToString();
                if (usrPwd == queryPassword)
                {
                    lbl_Message.Text = "验证成功";
                    Response.Redirect("welcome.aspx");
                }
                else
                {
                    lbl_Message.Text = "验证失败";
                }
            }
            else {
                lbl_Message.Text = "用户名错误";
            }
                //结束
                dr.Close();
                con.Close();
                


            //}
            //catch (MySqlException ex)
            //{
            //    Console.WriteLine(ex.Message);//有错则报出错误
            //}

            //finally
            //{

            //}



        }


        }
}
WebForm_Reg.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_Reg.aspx.cs" Inherits="WebApplication_OmtpcMgrSystem.sign.WebForm_Reg" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lb1" runat="server" Text="账号"></asp:Label>
            <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
            <br />
            <asp:Label ID="lb2" runat="server" Text="密码"></asp:Label>
            <asp:TextBox ID="tb2" runat="server" ></asp:TextBox>
            <br />
            <asp:Label ID="lb3" runat="server" Text="邀请码"></asp:Label>
            <asp:TextBox ID="tb3" runat="server"></asp:TextBox>
            <br />
            <asp:Label ID="lbl_Message" runat="server" Text=""></asp:Label>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <br />
            <asp:HyperLink ID="hre1" runat="server">已有账号?立即登录!</asp:HyperLink>
        </div>
    </form>
</body>
</html>
WebForm_Reg.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace WebApplication_OmtpcMgrSystem.sign
{
    public partial class WebForm_Reg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hre1.NavigateUrl = "WebForm_Login.aspx";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //接受前端数据并进行简单处理
            string usrName = tb1.Text.Trim();
            string usrPwd = tb2.Text.Trim();
            string addCode = tb3.Text.Trim();
            //验证数据是否合理
            if (usrName.Length == 0 || usrName.Length > 100)
            {
                lbl_Message.Text = "UserName is wrong!";
            };
            if (usrPwd.Length < 6 || usrPwd.Length > 100)
            {
                lbl_Message.Text = "UserPassword is wrong!";
            }
            if (usrPwd.Length < 0 || usrPwd.Length > 50)
            {
                lbl_Message.Text = "Invitation code is wrong!";
            }
            //try
            //{
            //设计连接字符串(连接数据库)
            string conn =
                "Data Source = 127.0.0.1;" +
                "User ID=root;" +
                "Password=qq2686485465;" +
                "DataBase=omtpc;" +
                "port=3306";
            //定义连接对象(构造函数的参数为数据库连接字符串)
            MySqlConnection con = new MySqlConnection(conn);
            //打开数据库连接
            con.Open();
            //执行数据库的访问操作
            string strSqlCommand = "Select*from invitationcode where code='" + addCode + "'";
            MySqlCommand cmd = new MySqlCommand(strSqlCommand, con);
            MySqlDataReader dr = cmd.ExecuteReader();//查找多行 : ExecuteReader()方法 | 执行结果放入dr中
                                                     //dr.Read();//读出dr内容

            if (dr.Read())
            {
                string queryPassword = dr["used"].ToString();
                if (queryPassword=="1")
                {
                    //此时,注册码是没有问题的,但是在将新账户写入数据库之前,应当先验证数据库中是否已存在该账号
                    strSqlCommand = "Select*from officer21 where usrID='" + usrName + "'";
                    con.Close();
                    con.Open();
                    cmd = new MySqlCommand(strSqlCommand, con);
                    dr = cmd.ExecuteReader();
                    if (dr.Read()) {
                        //此时说明账号已被注册
                        lbl_Message.Text = "该账号已被注册,您可以直接登录或更换账号注册";
                    }else{
                        //此时说明账号没被注册
                        //准备在数据库中写入数据
                        con.Close();
                        con.Open();
                        //更新数据库中的账户信息
                        strSqlCommand = "insert into officer21 values('" + usrName + "','" + usrName + "','" + usrPwd + "','','','','')";
                        cmd = new MySqlCommand(strSqlCommand, con);
                        var row1 = cmd.ExecuteNonQuery();
                        //更新数据库的邀请码信息
                        strSqlCommand = " update invitationcode set used = 0, usrID= '" + usrName + "'where code='"+addCode+"'";
                        cmd = new MySqlCommand(strSqlCommand, con); 
                        var row2 = cmd.ExecuteNonQuery();
                        if(row1>0 & row2>0)
                        lbl_Message.Text = "验证成功";

                    }
                }
                else
                {
                    //此时说明注册码已经被使用了
                    lbl_Message.Text = "验证失败";
                }
            }
            else
            {
                lbl_Message.Text = "邀请码不存在";
            }
            //结束
            dr.Close();
            con.Close();
            //}
            //catch (MySqlException ex)
            //{
            //    Console.WriteLine(ex.Message);//有错则报出错误
            //}

            //finally
            //{

            //}
        } 
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌狼蓝天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值