利用DataSet部分功能實現網站登錄
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZG.Common;
using System.Data;
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)
{
//用戶表 Sys_User 列PersonStatus為“正常”才可登錄,不然提示賬戶狀態為PersonStatus內的內容
//列PersonCode為用戶名Password為密碼
//數據庫中PassWord保存的為加密後的 字符串.Ext_DecryptString();為解密Ext_EncryptString();為加密
string userName=txtUserName.Text.Trim();
string passWord=txtPwd.Text.Trim();
//.Ext_IsNullOrEmpty()是在另一個文件中自己編寫的函數,用於判斷字符串是否為空字符
if(userName.Ext_IsNullOrEmpty())
{
ScriptHelper.ShowAlertScript("請輸入用戶名!");
return;
}
if(passWord.Ext_IsNullOrEmpty())
{
ScriptHelper.ShowAlertScript("請輸入密碼!");
return;
}
//在Sys_User 表中篩選出用戶名為userName的數據數量,如果為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("用戶名不存在!"0;
return;
}
//在SYS_User表中篩選出用戶名為userName 的PersonStatus值
DataSet dsStatus=SqlHelper.GetData("select PersonStatus from Sys_User where PersonCode‘"+userName+"‘");
//取出dsStatus(小數據庫)中([0])第一張表的第一行中名為PersonStatus的列的值
string personStatus=dsStatus.Tables[0].Rows[0]["PersonStatus"].ToString();
if(personStatus !="正常")
{
ScriptHelper.ShowAlertScript("用戶狀態不正確:"+personStatus);
return;
}
//註意密碼的加密,空字符加密後便不是空字符了。數據庫中的密碼是加密後的字符,實際比較中需要用實際輸入字符經加密得到的字符與數據庫中的比較
//判斷密碼 法一
//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["PersonName"].ToString();
Session["UserID"]=dsUser.Tables[0].Rows[0]["ItemID"].ToString();
//如果登錄成功,跳轉到首頁
Response.Redirect("index.aspx");
}
}
}
利用DataSet部分功能實現網站登錄