ASP.NET簡單登錄注冊實例


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <table>
        <tr>
            <td> <asp:Label ID="Label1" runat="server" Text="用戶名:"></asp:Label> </td>
            <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        </tr>
          <tr>
            <td> <asp:Label ID="Label2" runat="server" Text="密碼:"></asp:Label> </td>
            <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
        </tr>
         <tr>
            <td><asp:Button ID="Button2" runat="server" Text="登錄" onclick="Button2_Click" /> </asp:Label> </td>
            <td> <asp:Button ID="Button1" runat="server" Text="注冊" onclick="Button1_Click" /> </asp:TextBox></td>
        </tr>
       </table>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //登錄
        protected void Button2_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            if (name!=null||pwd!=null)
            {
                string sql = "select count(0) from student where stuName=@a and stuNo=@b";//獲取數據中的信息
                SqlParameter p1 = new SqlParameter("@a",name);
                SqlParameter p2 = new SqlParameter("@b",pwd);
                //連接數據庫
                string connStr = "server=DESKTOP-QQGOIKH;uid=sa;pwd=123;database=stuDB";
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql,conn);
                cmd.Parameters.Add(p1);
                cmd.Parameters.Add(p2);
                object obj = cmd.ExecuteScalar();
                conn.Close();
                int i = (int)obj;
                if (i>0)
                {
                    Response.Redirect("WebForm2.aspx");
                }
                else
                {
                    Response.Write("登錄失敗");
                }
            }
            else
            {
                return;
            }
                
            
        }
        //注冊
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm3.aspx");

        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td><asp:Label ID="Label1" runat="server" Text="用戶名:"></asp:Label> </td>
            <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Label ID="Label2" runat="server" Text="密碼:"></asp:Label> </td>
            <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td>
        </tr>
        <tr>
            <td> <asp:Button ID="Button1" runat="server" Text="注冊" onclick="Button1_Click" /> </td>
            <td> <asp:Button ID="Button2" runat="server" Text="返回" onclick="Button2_Click" /> </td>
        </tr>
    </table> 
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //注冊
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            if (name!=null||pwd!=null)
            {
                string sql = "insert into enter values(@a,@b)";
                SqlParameter[] sp = new SqlParameter[2];
                sp[0] = new SqlParameter("@a",TextBox1.Text);
                sp[1] = new SqlParameter("@b",TextBox2.Text);
                SqlConnection conn = new SqlConnection("server=STER-PC;uid=sa;pwd=123;database=t2");
                conn.Open();
                SqlCommand comm = new SqlCommand(sql,conn);
                comm.Parameters.AddRange(sp);//添加數組
                int i = comm.ExecuteNonQuery();
                conn.Close();
                if (i>0)
                {
                    Response.Write("注冊成功");
                }
                else
                {
                    Response.Write("注冊失敗");
                }
            }
            else
            {
                Response.Write("請填寫內容");
            }
        }
        //返回
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm1.aspx");
        }
    }
}

 


免責聲明!

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



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