【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;
相关文章
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
795 158
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2067 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1423 156
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
SQL 关系型数据库 MySQL
Mysql数据恢复—Mysql数据库delete删除后数据恢复案例
本地服务器,操作系统为windows server。服务器上部署mysql单实例,innodb引擎,独立表空间。未进行数据库备份,未开启binlog。 人为误操作使用Delete命令删除数据时未添加where子句,导致全表数据被删除。删除后未对该表进行任何操作。需要恢复误删除的数据。 在本案例中的mysql数据库未进行备份,也未开启binlog日志,无法直接还原数据库。
1139 2
|
XML 前端开发 C#
C#编程实践:解析HTML文档并执行元素匹配
通过上述步骤,可以在C#中有效地解析HTML文档并执行元素匹配。HtmlAgilityPack提供了一个强大而灵活的工具集,可以处理各种HTML解析任务。
602 19
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
572 3
|
监控 算法 C#
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
2767 0
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
1709 12

推荐镜像

更多