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

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 【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
            //{
            //}
        } 
    }
}


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9天前
|
关系型数据库 MySQL 数据库连接
Unity连接Mysql数据库 增 删 改 查
在 Unity 中连接 MySQL 数据库,需使用 MySQL Connector/NET 作为数据库连接驱动,通过提供服务器地址、端口、用户名和密码等信息建立 TCP/IP 连接。代码示例展示了如何创建连接对象并执行增删改查操作,确保数据交互的实现。测试代码中,通过 `MySqlConnection` 类连接数据库,并使用 `MySqlCommand` 执行 SQL 语句,实现数据的查询、插入、删除和更新功能。
|
24天前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
1天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
40 25
|
25天前
|
存储 安全 API
陪玩平台中支付与结算模块的代码,陪玩系统数据库设计与代码实现
第三方支付平台对接涉及与微信支付、支付宝等API接口的调用,确保用户支付流程顺畅。结算模块根据业务规则计算陪玩师收益,强调安全性、异常处理、可扩展性和日志记录。数据库设计涵盖用户、陪玩者、订单等信息的存储管理,确保系统稳定运行。
|
28天前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
52 2
|
2月前
|
SQL Java 数据库连接
JDBC编程安装———通过代码操控数据库
本文,教你从0开始学习JBCD,包括驱动包的下载安装调试设置,以及java是如何通过JBDC实现对数据库的操作,以及代码的分析,超级详细
|
10天前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
82 42
|
28天前
|
缓存 关系型数据库 MySQL
【深入了解MySQL】优化查询性能与数据库设计的深度总结
本文详细介绍了MySQL查询优化和数据库设计技巧,涵盖基础优化、高级技巧及性能监控。
229 0
|
2月前
|
存储 Oracle 关系型数据库
数据库传奇:MySQL创世之父的两千金My、Maria
《数据库传奇:MySQL创世之父的两千金My、Maria》介绍了MySQL的发展历程及其分支MariaDB。MySQL由Michael Widenius等人于1994年创建,现归Oracle所有,广泛应用于阿里巴巴、腾讯等企业。2009年,Widenius因担心Oracle收购影响MySQL的开源性,创建了MariaDB,提供额外功能和改进。维基百科、Google等已逐步替换为MariaDB,以确保更好的性能和社区支持。掌握MariaDB作为备用方案,对未来发展至关重要。
73 3
|
2月前
|
安全 关系型数据库 MySQL
MySQL崩溃保险箱:探秘Redo/Undo日志确保数据库安全无忧!
《MySQL崩溃保险箱:探秘Redo/Undo日志确保数据库安全无忧!》介绍了MySQL中的三种关键日志:二进制日志(Binary Log)、重做日志(Redo Log)和撤销日志(Undo Log)。这些日志确保了数据库的ACID特性,即原子性、一致性、隔离性和持久性。Redo Log记录数据页的物理修改,保证事务持久性;Undo Log记录事务的逆操作,支持回滚和多版本并发控制(MVCC)。文章还详细对比了InnoDB和MyISAM存储引擎在事务支持、锁定机制、并发性等方面的差异,强调了InnoDB在高并发和事务处理中的优势。通过这些机制,MySQL能够在事务执行、崩溃和恢复过程中保持
120 3