一.登錄頁面及後臺代碼
1.登錄頁面如圖1所示
首先進行身份選擇,由“管理員”和“用戶”兩種身份進行選擇,選擇不同的身份,程序會進入不同的數據表檢索登錄信息;當用戶名或密碼爲空時會提示;當用戶名或密碼在數據表中沒有檢索到時,會提示登錄失敗;
有一些細節優化,在程序中有所體現:
(1)由“管理員”和“用戶”兩種身份進行選擇,選擇不同的身份,程序會進入不同的數據表檢索登錄信息;
(2)管理員和用戶兩種身份登錄,會跳轉到不同的頁面;//Response.Redirect("AdminPage.aspx");
(3)使用session暫存用戶名信息,實現不同頁面之間的信息共享,在另一個頁面直接應用session即可;//Session["LoginName"] = TextBox1.Text;
圖1
2.後臺代碼如下:
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 Button1_Click(object sender, EventArgs e)
{
//連接數據庫
if (DropDownList1.Text == "管理員")
{
SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
con.Open();
//定義字符串sql,其含義爲從數據表中查找列LoginName中TextBox1.Text的記錄,列Password中TextBox2.Text的記錄
string sql = "select * from AdminInfo where LoginName= '" + TextBox1.Text + "' and Password= '" + TextBox2.Text + "' ";
//定義數據適配器da,將da的數據填充至Dataset類的對象dt中
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(dt);
//如果記錄爲TextBox1.Text和TextBox2.Text的行已經存在
if (dt.Rows.Count > 0)
{
//將TextBox1.Text的信息暫存
Session["LoginName"] = TextBox1.Text;
string count = Session["LoginName"].ToString();
//如果以管理員賬號進,就轉到AdminPage.aspx
Response.Redirect("AdminPage.aspx");
}
//用戶輸入的內容不存在於數據庫中
else
{
Label1.Text = "您輸入的用戶名或密碼錯誤,登錄失敗!";
}
con.Close();
}
if (DropDownList1.Text == "用戶")
{
SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
con.Open();
//定義字符串sql,其含義爲從數據表中查找列LoginName中TextBox1.Text的記錄,列Password中TextBox2.Text的記錄
string sql = "select * from UserInfo where UserName= '" + TextBox1.Text + "' and Password= '" + TextBox2.Text + "' ";
//定義數據適配器da,將da的數據填充至Dataset類的對象dt中
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(dt);
//如果記錄爲TextBox1.Text和TextBox2.Text的行已經存在
if (dt.Rows.Count > 0)
{
//將TextBox1.Text的信息暫存
Session["UserName"] = TextBox1.Text;
string count = Session["UserName"].ToString();
//如果不是管理員賬號,即轉入
Response.Redirect("UserPage.aspx");
}
//用戶輸入的內容不存在於數據庫中
else
{
Label1.Text = "您輸入的用戶名或密碼錯誤,登錄失敗!";
}
con.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
Label1.Text = "";
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
}
}
二.登錄頁面及後臺代碼
1.註冊頁面如圖2所示:
(1)輸入用戶名時,連接數據庫驗證用戶名是否已經存在,若存在會提示信息
(2)驗證輸入是否爲空,使用RequiredFieldValidator控件;驗證輸入是否爲郵箱格式,使用RegularExpressionValidator控件
圖2
2.程序後臺代碼如下:
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;
using System.Windows.Forms;
namespace WebApplication1
{
public partial class Register1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//驗證用戶名是否存在
private bool CheckUser()
{
//連接數據庫
SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
con.Open();
//定義字符串sql,其含義爲從數據表中查找列UserName中TextBox1.Text的記錄
string sql = "select * from UserInfo where UserName= '" + TextBox11.Text + "' ";
//定義數據適配器da,將da的數據填充至Dataset類的對象dt中
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(dt);
//如果記錄爲TextBox1.Text的行已經存在
if (dt.Rows.Count > 0)
{
Label11.Text = " 該用戶名已存在";
return false;
}
else
{
return true;
}
con.Close();
}
//註冊按鈕
protected void Button34_Click(object sender, EventArgs e)
{
//檢查用戶名是否已經存在
if (CheckUser() == false)
{
return;
}
//用戶名未註冊
SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("", con);
//在數據表中插入用戶輸入的註冊信息
cmd.CommandText = "INSERT INTO UserInfo(UserName,Password,Email)VALUES('" + TextBox11.Text.Trim() + "','" + TextBox22.Text.Trim() + "','" + TextBox4.Text.Trim() + "')";
cmd.ExecuteNonQuery();
con.Close();
//MessageBox.Show("註冊成功,請點擊“確定”返回登錄界面!", "溫馨提示", MessageBoxButtons.YesNo);
Response.Redirect("Login.aspx");
}
//重置按鈕
protected void Button35_Click(object sender, EventArgs e)
{
TextBox11.Text = "";
TextBox22.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
//返回按鈕
protected void Button36_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
}
}