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

时间:2023-03-10 03:52:47
【C#】【MySQL】C#连接MySQL数据库(三)登陆注册代码

项目结构

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

项目代码

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