乾貨——asp.net快速上手教程7項目實戰3

乾貨——asp.net快速上手教程7項目實戰3

圖書管理系統

本次內容

上一次可我們已經完成了系統框架的搭建,現在我們可以開始實現這個系統具體的功能

任務

實現系統的登錄功能

開始

登錄界面請參考AdminLTE中的登錄頁面(AdminLTE-2.3.11\pages\examples\login.html)

界面關鍵代碼

用戶名輸入框

密碼輸入框

身份選擇框

工作人員

普通用戶

補充說明:DropDownList (下拉列表框),詳細用法參照項目中的使用

乾貨——asp.net快速上手教程7項目實戰3

登錄界面

後臺驗證登錄

關鍵數據庫代碼

select * from LMS_Admin where (admin_mail=@userName or admin_phone=@userName) and admin_pwd = @pwd;--管理員登錄

select * from LMS_Users where (u_mail = @userName or u_phone=@userName) and u_pwd=@pwd;--普通用戶登錄

測試數據

insert into LMS_Admin values('騎豬豬的管理員','123','123','123','[email protected]');

insert into LMS_Users values('騎豬豬的普通用戶','1234','1234','1234','[email protected]','1234');

後臺關鍵代碼(登錄按鈕的點擊事件)

protected void btn_ok_Click(object sender, EventArgs e)

{

string userName = tb_userName.Text;//獲取用戶名

string password = tb_password.Text;//獲取密碼

string role = ddl_role.SelectedValue;//獲取用戶的登錄省份

string cmd = "";

if(role.Equals("1"))//管理員

{

cmd = "select * from LMS_Admin where (admin_mail=@userName or admin_phone=@userName) and admin_pwd = @pwd;";//在管理員表中查詢數據

}

else//普通用戶

{

cmd = "select * from LMS_Users where (u_mail = @userName or u_phone=@userName) and u_pwd=@pwd;";//在普通用戶表中查詢數據

}

Dictionary par = new Dictionary();//新建一個字典對象

par.Add("@userName", userName);//將用戶名作為參數添加進入字典

par.Add("@pwd", password);//將密碼作為參數添加進入字典

MicrosoftSQLServer db = new MicrosoftSQLServer(SystemInfo.conString);//新建數據庫操作對象

DataTable userInfo = db.Execute_Select_ResultTable(cmd, par, false);//在數據庫中查詢數據並返回DataTable

Hashtable user = new Hashtable();

if(userInfo.Rows.Count>0)

{

for(int i =0;i< userInfo.Columns.Count;i++)

{

user.Add(userInfo.Columns[i].ColumnName, userInfo.Rows[0][i].ToString());//將用戶信息保存到hash表中

}

Session["user"] = user;//將用戶信息保存到Session會話中

Response.Redirect("Demo.aspx");//登錄成功,跳轉到成功頁面

}

else

{

Response.Redirect("login.aspx");//登錄失敗,跳轉回登錄頁面

}

}

這樣,我們就完成了登錄的功能。

最後的話

代碼上我都加上了比較詳細的註釋,如果有哪一段做的不夠細緻不便於理解的話,歡迎在評論區留言,我會在下一次教程中專門拿出來解釋,謝謝大家能看到這~~~


分享到:


相關文章: