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

            //}
        } 
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM