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

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

项目结构


q1.png

项目代码

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
            //{
            //}
        } 
    }
}


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
12月前
|
存储 关系型数据库 数据库
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用
本文通过一个 Agentic RAG 应用的完整构建流程,展示了如何借助 RDS Supabase 快速搭建具备知识处理与智能决策能力的 AI 应用,展示从数据准备到应用部署的全流程,相较于传统开发模式效率大幅提升。
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用
|
人工智能 安全 机器人
无代码革命:10分钟打造企业专属数据库查询AI机器人
随着数字化转型加速,企业对高效智能交互解决方案的需求日益增长。阿里云AppFlow推出的AI助手产品,借助创新网页集成技术,助力企业打造专业数据库查询助手。本文详细介绍通过三步流程将AI助手转化为数据库交互工具的核心优势与操作指南,包括全场景适配、智能渲染引擎及零代码配置等三大技术突破。同时提供Web集成与企业微信集成方案,帮助企业实现便捷部署与安全管理,提升内外部用户体验。
1102 12
无代码革命:10分钟打造企业专属数据库查询AI机器人
|
10月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1578 152
|
10月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1098 156
|
安全 druid Nacos
0 代码改造实现应用运行时数据库密码无损轮转
本文探讨了敏感数据的安全风险及降低账密泄漏风险的策略。国家颁布的《网络安全二级等保2.0标准》强调了企业数据安全的重要性。文章介绍了Nacos作为配置中心在提升数据库访问安全性方面的应用,并结合阿里云KMS、Druid连接池和Spring Cloud Alibaba社区推出的数据源动态轮转方案。该方案实现了加密配置统一托管、帐密全托管、双层权限管控等功能,将帐密切换时间从数小时优化到一秒,显著提升了安全性和效率。未来,MSE Nacos和KMS将扩展至更多组件如NoSQL、MQ等,提供一站式安全服务,助力AI时代的应用安全。
665 14
|
SQL 数据建模 关系型数据库
别光知道存数据库了,数据建模才是王道!(入门指南+实战代码)
别光知道存数据库了,数据建模才是王道!(入门指南+实战代码)
2957 4
|
10月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
10月前
|
SQL 关系型数据库 MySQL
Mysql数据恢复—Mysql数据库delete删除后数据恢复案例
本地服务器,操作系统为windows server。服务器上部署mysql单实例,innodb引擎,独立表空间。未进行数据库备份,未开启binlog。 人为误操作使用Delete命令删除数据时未添加where子句,导致全表数据被删除。删除后未对该表进行任何操作。需要恢复误删除的数据。 在本案例中的mysql数据库未进行备份,也未开启binlog日志,无法直接还原数据库。
|
12月前
|
安全 Java Nacos
0代码改动实现Spring应用数据库帐密自动轮转
Nacos作为国内被广泛使用的配置中心,已经成为应用侧的基础设施产品,近年来安全问题被更多关注,这是中国国内软件行业逐渐迈向成熟的标志,也是必经之路,Nacos提供配置加密存储-运行时轮转的核心安全能力,将在应用安全领域承担更多职责。

推荐镜像

更多