您好,登錄后才能下訂單哦!
小編給大家分享一下如何利用DataSet部分功能實(shí)現(xiàn)網(wǎng)站登錄,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
首先,我之前必須完成過(guò)注冊(cè),并把個(gè)人信息存入數(shù)據(jù)庫(kù)中。
其次,這部分的個(gè)別對(duì)象是存于某些文檔中的,需要引用命名空間。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ZG.Common;//后面用到ScriptHelper對(duì)象(ScriptHelper.cs是自己編寫(xiě)的cs文件) using System.Data;//后面用到dataset namespace WebApplication { public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 登錄按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnLogin_Click(object sender, EventArgs e) { //用戶(hù)表 Sys_User 列PersonStatus 為 “正?!?nbsp;才可登錄 不然提示賬戶(hù)狀態(tài)為PersonStatus內(nèi)的內(nèi)容 //列PersonCode為用戶(hù)名 PassWord為密碼 //數(shù)據(jù)庫(kù)中PassWord保存的為加密后的 字符串.Ext_DecryptString();為解密 Ext_EncryptString();為加密 string userName = txtUserName.Text.Trim();//.Trim()是去掉字符串前后的空字符 string passWord = txtPwd.Text.Trim(); //.Ext_IsNullOrEmpty()是在另一個(gè)文件中自己編寫(xiě)的函數(shù),用于判斷字符串是否為空字符(也可用userName==“”等判斷) if (userName.Ext_IsNullOrEmpty()) { ScriptHelper.ShowAlertScript("請(qǐng)輸入用戶(hù)名!");//彈出窗體提示 return; } if (passWord.Ext_IsNullOrEmpty()) { ScriptHelper.ShowAlertScript("請(qǐng)輸入密碼!"); return; } //在Sys_User 表中篩選出用戶(hù)名為userName的數(shù)據(jù)數(shù)量,如果為0表示沒(méi)有該用戶(hù),為1表示有。 DataSet ds = SqlHelper.GetData("select count(*) from Sys_User where PersonCode='" + userName+ "'"); if (ds.Tables[0].Rows[0][0].ToString() != "1") { ScriptHelper.ShowAlertScript("用戶(hù)名不存在!"); return; } //在Sys_User 表中篩選出用戶(hù)名為userName的PersonStatus 值。 DataSet dsStatus = SqlHelper.GetData("select PersonStatus from Sys_User where PersonCode='" + userName + "'"); //取出dsStatus(小數(shù)據(jù)庫(kù))中([0])第一張表的第一行中名為PersonStatus的列的值 string personStatus = dsStatus.Tables[0].Rows[0]["PersonStatus"].ToString(); if (personStatus != "正常") { ScriptHelper.ShowAlertScript("用戶(hù)狀態(tài)不正確:" + personStatus); return; } //注意密碼的加密,空字符加密后便不是空字符了。數(shù)據(jù)庫(kù)中的密碼是加密后的字符,實(shí)際比較中需要用實(shí)際輸入字符經(jīng)加密得到的字符與數(shù)據(jù)庫(kù)中的比較 //判斷密碼 法一 //string sql = "select * from Sys_User where PersonCode='{0}' and Password='{1}'"; //DataSet dsUser = SqlHelper.GetData(string.Format(sql, userName, passWord.Ext_EncryptString())); //if (dsUser.Tables[0].Rows.Count!=1) //{ // ScriptHelper.ShowAlertScript("密碼不正確!"); // return; //} //判斷密碼 法二 string sql = "select * from Sys_User where PersonCode='{0}' "; DataSet dsUser = SqlHelper.GetData(string.Format(sql, userName)); if (dsUser.Tables[0].Rows[0]["PassWord"].ToString() != passWord.Ext_EncryptString()) { ScriptHelper.ShowAlertScript("密碼不正確!"); return; } Session["UserName"] = dsUser.Tables[0].Rows[0]["PersonCode"].ToString(); Session["LoginUser"] = dsUser.Tables[0].Rows[0]["PersonName"].ToString(); Session["UserID"] = dsUser.Tables[0].Rows[0]["ItemID"].ToString(); //如果登錄成功 跳轉(zhuǎn)到首頁(yè) Response.Redirect("index.aspx"); } } }
看完了這篇文章,相信你對(duì)“如何利用DataSet部分功能實(shí)現(xiàn)網(wǎng)站登錄”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。