首先,我之前必須完成過注冊,并把個人信息存入數(shù)據(jù)庫中。
其次,這部分的個別對象是存于某些文檔中的,需要引用命名空間。
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對象(ScriptHelper.cs是自己編寫的cs文件)using System.Data;//后面用到datasetnamespace 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) {//用戶表 Sys_User 列PersonStatus 為 “正常” 才可登錄 不然提示賬戶狀態(tài)為PersonStatus內(nèi)的內(nèi)容//列PersonCode為用戶名 PassWord為密碼//數(shù)據(jù)庫中PassWord保存的為加密后的 字符串.Ext_DecryptString();為解密 Ext_EncryptString();為加密string userName = txtUserName.Text.Trim();//.Trim()是去掉字符串前后的空字符string passWord = txtPwd.Text.Trim();//.Ext_IsNullOrEmpty()是在另一個文件中自己編寫的函數(shù),用于判斷字符串是否為空字符(也可用userName==“”等判斷)if (userName.Ext_IsNullOrEmpty()) { ScriptHelper.ShowAlertScript("請輸入用戶名!");//彈出窗體提示return; }if (passWord.Ext_IsNullOrEmpty()) { ScriptHelper.ShowAlertScript("請輸入密碼!");return; }//在Sys_User 表中篩選出用戶名為userName的數(shù)據(jù)數(shù)量,如果為0表示沒有該用戶,為1表示有。DataSet ds = SqlHelper.GetData("select count(*) from Sys_User where PersonCode='" + userName+ "'"); if (ds.Tables[0].Rows[0][0].ToString() != "1") { ScriptHelper.ShowAlertScript("用戶名不存在!");return; }//在Sys_User 表中篩選出用戶名為userName的PersonStatus 值。DataSet dsStatus = SqlHelper.GetData("select PersonStatus from Sys_User where PersonCode='" + userName + "'");//取出dsStatus(小數(shù)據(jù)庫)中([0])第一張表的第一行中名為PersonStatus的列的值string personStatus = dsStatus.Tables[0].Rows[0]["PersonStatus"].ToString(); if (personStatus != "正常") {ScriptHelper.ShowAlertScript("用戶狀態(tài)不正確:" + personStatus); return; }//注意密碼的加密,空字符加密后便不是空字符了。數(shù)據(jù)庫中的密碼是加密后的字符,實(shí)際比較中需要用實(shí)際輸入字符經(jīng)加密得到的字符與數(shù)據(jù)庫中的比較//判斷密碼 法一 //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)到首頁Response.Redirect("index.aspx"); } }}
以上所述是小編給大家介紹的利用DataSet部分功能實(shí)現(xiàn)網(wǎng)站登錄 ,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
新聞熱點(diǎn)
疑難解答
圖片精選