利用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");

}

}

}

posted @ 2017-06-26 15:17  维尼熊320  阅读(181)  评论(0编辑  收藏  举报