基于ADO.NET的用戶登陸與注冊系統
1. 創建資料表users
1.1 users表的設計
1.2 SQL代碼
2. 用戶登陸功能
2.1 登陸界面設計
2.2 修改控制元件ID和屬性
2.3 登陸按鈕功能實作
2.4 注冊按鈕功能實作
2.5 忘記密碼按鈕功能實作
3. 用戶注冊功能
3.1 注冊界面設計
3.2 修改控制元件屬性
3.3注冊功能的實作
3.4回傳按鈕功能的實作
4. 找回密碼功能
4.1 找回密碼界面設計
4.2 修改控制元件屬性
4.3 頁面加載事件
4.4 重置密碼功能實作
4.5 回傳按鈕功能實作
5. 用戶登陸
5.1 管理員登錄
5.1.1 管理員界面設計
5.1.2 配置資料源
5.2 普通用戶登陸
6. 補充和完善
7. 系統下載地址
摘要:基于ASP.NET的WEB應用程式專案,使用程式語言C#,利用ADO.NET訪問資料庫,實作一個簡易的用戶登陸注冊系統,主要實作的功能有用戶登陸、用戶注冊、找回密碼,軟體版本采用的vs2010加Sql Sever2014,
關鍵字:ASP.NET;ADO.NET;WEB;vs2010;資料庫
1. 創建資料表users
1.1 users表的設計
users表的表結構設計如圖1-1-1,users表中的資料填充如圖1-1-2,

圖1-1-1

圖1-1-2
1.2 SQL代碼
CREATE TABLE users
(
username NCHAR(10) PRIMARY KEY NOT NULL,
userpwd NCHAR(10) NOT NULL,
level NCHAR(10) NOT NULL,
question NCHAR(20) NOT NULL,
answer NCHAR(20) NOT NULL
)
INSERT INTO users VALUES('花花','123456','admin','國家','中國');
INSERT INTO users VALUES('貝貝','654321','user','省份','廣東');
INSERT INTO users VALUES('寶寶','456789','user','城市','廣州');
2. 用戶登陸功能
2.1 登陸界面設計
新建一個Web表單命名為login.aspx,打開login.aspx檔案,切換至設計視圖,插入表(4行2列),從工具列中拉入2個TextBox控制元件和3個Button控制元件到界面上,頁面布局如圖2-1-1,

圖2-1-1
2.2 修改控制元件屬性
登陸界面源代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 279px;
}
.style2
{
color: #0066FF;
height: 24px;
}
.style3
{
text-align:left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td class="style2" colspan="2" style="text-align: center">
用戶登陸</td>
</tr>
<tr>
<td class="style3">
用戶名</td>
<td>
<asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
密碼</td>
<td>
<asp:TextBox ID="txt_pwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btn_login" runat="server" Text="登陸" onclick="btn_login_Click" />
<asp:Button ID="btn_register" runat="server" Text="注冊"
onclick="btn_register_Click" />
<asp:Button ID="btn_forget" runat="server" Text="忘記密碼"
onclick="btn_forget_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
2.3 登陸按鈕功能實作
雙擊登陸按鈕,創建Click事件,撰寫功能代碼如下:
protected void btn_login_Click(object sender, EventArgs e)
{
string strName = txt_username.Text;
Session["username"] = strName;
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.\\sqlexpress;database=SYSTEM01;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from users where username='" +txt_username.Text + "' and userpwd='" + txt_pwd.Text + "'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
ds.Clear();
con.Open();
da.Fill(ds, "users");
con.Close();
if (ds.Tables["users"].Rows.Count == 0)
{
Response.Write("<script>alert('用戶名或密碼錯!')</script>");
return;
}
DataRow dr = ds.Tables["users"].Rows[0];
if (dr[2].ToString().Trim() == "admin")
Response.Write("<script>alert('你是管理員,歡迎');window.location.href='admin.aspx'</script>");
else
Response.Write("<script>alert('你是普通用戶,歡迎');window.location.href='users.aspx'</script>");
}
2.4 注冊按鈕功能實作
雙擊注冊按鈕,創建Click事件,撰寫功能代碼如下:
protected void btn_register_Click(object sender, EventArgs e)
{
Response.Redirect("register.aspx");
}
2.5 忘記密碼按鈕功能實作
雙擊忘記密碼按鈕,創建Click事件,撰寫功能代碼如下:
protected void btn_forget_Click(object sender, EventArgs e)
{
if (txt_username.Text == "")
{
Response.Write("<script>alert('請輸入用戶名')</script>");
return;
}
Response.Redirect("forget.aspx?username=" + txt_username.Text);
}
3.用戶注冊功能
3.1 注冊界面設計
新建一個Web表單,命名為register.aspx,切換至設計視圖,插入表(7行兩列,拉入3個TextBox控制元件、2個DropDownList控制元件和2個Button控制元件到界面上,界面布局如圖3-1-1,

圖3-1-1
3.2 修改控制元件屬性
用戶注冊源代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 500px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2" style="text-align: center; color: #3366FF">
用戶注冊</td>
</tr>
<tr>
<td>
用戶名</td>
<td>
<asp:TextBox ID="r_username" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
密碼</td>
<td>
<asp:TextBox ID="r_pwd" runat="server" TextMode="Password" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
身份</td>
<td>
<asp:DropDownList ID="DDL_id" runat="server">
<asp:ListItem Value="admin">管理員</asp:ListItem>
<asp:ListItem Value="user">普通用戶</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
密保問題</td>
<td>
<asp:DropDownList ID="DDL_question" runat="server">
<asp:ListItem Value="國家">國家?</asp:ListItem>
<asp:ListItem Value="省份">省份?</asp:ListItem>
<asp:ListItem Value="城市">城市?</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
密保答案</td>
<td>
<asp:TextBox ID="txt_answer" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btn_register" runat="server" Text="注冊"
onclick="btn_register_Click" />
<asp:Button ID="btn_return" runat="server" Text="回傳" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
3.3 注冊功能的實作
雙擊注冊按鈕,創建Click事件,撰寫功能代碼如下:
protected void btn_register_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.\\sqlexpress;database=SYSTEM01;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into users(username,userpwd,level,question,answer) values (@username,@userpwd,@level,@question,@answer)";
cmd.Parameters.Add("@username", SqlDbType.NChar, 10);
cmd.Parameters.Add("@userpwd", SqlDbType.NChar, 10);
cmd.Parameters.Add("@level", SqlDbType.NChar, 10);
cmd.Parameters.Add("@question", SqlDbType.NChar, 20);
cmd.Parameters.Add("@answer", SqlDbType.NChar, 20);
cmd.Parameters["@username"].Value =r_username.Text;
cmd.Parameters["@userpwd"].Value = r_pwd.Text;
cmd.Parameters["@level"].Value = DDL_id.SelectedValue;
cmd.Parameters["@question"].Value = DDL_question.SelectedValue;
cmd.Parameters["@answer"].Value = txt_answer.Text;
con.Open();
if (cmd.ExecuteNonQuery() > 0)
Response.Write("資料新增成功");
//Response.Redirect("admin.aspx");
else
Response.Write("資料新增失敗");
con.Close();
}
3.4 回傳按鈕功能的實作
protected void btn_return_Click(object sender, EventArgs e)
{
Response.Redirect("login.aspx");
}
4. 找回密碼功能
4.1 找回密碼界面設計
找回密碼界面布局如圖4-1-1,

圖4-1-1
4.2 修改控制元件屬性
找回密碼界面源代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 500px;
}
.style2
{
text-align: center;
color: #9933FF;
}
.style3
{
height: 27px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2" colspan="2">
找回密碼</td>
</tr>
<tr>
<td>
用戶名</td>
<td>
<asp:Label ID="L_username" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
密保問題</td>
<td>
<asp:Label ID="L_question" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
密保答案</td>
<td class="style3">
<asp:TextBox ID="T_answer" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="b_reset" runat="server" Text="重置密碼" onclick="b_reset_Click" />
<asp:Button ID="b_return" runat="server" Text="回傳" onclick="b_return_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
4.3 頁面加載事件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["username"] == null)
Response.Redirect("login.aspx");
}
L_username.Text = Request.QueryString["username"];
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.\\sqlexpress;database=SYSTEM01;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from users where username='" + L_username.Text + "'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
con.Open();
da.Fill(ds, "users");
con.Close();
if (ds.Tables["users"].Rows.Count == 0)
{
Response.Write("<script>alert('用戶名不存在')</script>");
return;
}
DataRow dr = ds.Tables["USERS"].Rows[0];
L_question.Text = dr[3].ToString().Trim();
}
4.4 重置密碼功能實作
protected void b_reset_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.\\sqlexpress;database=SYSTEM01;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from users where username='" + L_username.Text + "'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
con.Open();
da.Fill(ds, "users");
con.Close();
DataTable dt = new DataTable();
dt = ds.Tables["users"];
DataRow dr = dt.Rows[0];
if (T_answer.Text == dr[4].ToString().Trim())
{
SqlCommandBuilder scb = new SqlCommandBuilder(da);
Random r = new Random();
string newpwd = r.Next(100000, 999999).ToString();
Response.Write("<script>alert('你的新密碼是:" + newpwd + ",請牢記并及時更改!')</script>");
dr["userpwd"] = newpwd;
da.Update(dt);
}
else
Response.Write("<script>alert('你的提示問題答案不正確')</script>");
}
4.5 回傳按鈕功能實作
protected void b_return_Click(object sender, EventArgs e)
{
Server.Transfer("login.aspx");
}
5.用戶登陸
5.1 管理員登錄
5.1.1 管理員界面設計
新建一個“admin.aspx”的Web表單,切換至設計視圖,從工具列的資料組中拉入一個GridView控制元件,如圖5-1-1,

圖5-1-1
5.1.2 配置資料源
在智能選單中的有一個配置資料源的下拉框,選擇“新建資料源”,彈出以下,參考圖5-1-2至5-1-7操作步驟,

圖5-1-2

圖5-1-3

圖5-1-4

圖5-1-5

圖5-1-6

圖5-1-7
5.2 普通用戶登陸
在“users.aspx.cs”代碼的頁面加載部分撰寫如下代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
Response.Redirect("login.aspx");
}
Response.Write("歡迎"+Session["username"]+"登陸傻瓜系統!");
}
6. 補充和完善
從工具列的html組中分別給“admin.aspx”和“users.aspx”拉入1個按鈕控制元件,源代碼中新增修改的代碼如下:
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
window.location.href = "login.aspx";
}
// ]]>
</script>
<p>
<input id="Button1" type="button" value="回傳" onclick="return Button1_onclick()" />
</p>
7.系統下載地址
https://download.csdn.net/download/qq_40795187/12910519
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/156108.html
標籤:其他
