利用DataSet部分功能实现网站登录
首先,我之前必须完成过注册,并把个人信息存入数据库中。
其次,这部分的个别对象是存于某些文档中的,需要引用命名空间。
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingZG.Common;//后面用到ScriptHelper对象(ScriptHelper.cs是自己编写的cs文件) usingSystem.Data;//后面用到dataset namespaceWebApplication { publicpartialclassLogin:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { } //////登录按钮 /// ////// protectedvoidbtnLogin_Click(objectsender,EventArgse) { //用户表Sys_User列PersonStatus为“正常”才可登录不然提示账户状态为PersonStatus内的内容 //列PersonCode为用户名PassWord为密码 //数据库中PassWord保存的为加密后的字符串.Ext_DecryptString();为解密Ext_EncryptString();为加密 stringuserName=txtUserName.Text.Trim();//.Trim()是去掉字符串前后的空字符 stringpassWord=txtPwd.Text.Trim(); //.Ext_IsNullOrEmpty()是在另一个文件中自己编写的函数,用于判断字符串是否为空字符(也可用userName==“”等判断) if(userName.Ext_IsNullOrEmpty()) { ScriptHelper.ShowAlertScript("请输入用户名!");//弹出窗体提示 return; } if(passWord.Ext_IsNullOrEmpty()) { ScriptHelper.ShowAlertScript("请输入密码!"); return; } //在Sys_User表中筛选出用户名为userName的数据数量,如果为0表示没有该用户,为1表示有。 DataSetds=SqlHelper.GetData("selectcount(*)fromSys_UserwherePersonCode='"+userName+"'"); if(ds.Tables[0].Rows[0][0].ToString()!="1") { ScriptHelper.ShowAlertScript("用户名不存在!"); return; } //在Sys_User表中筛选出用户名为userName的PersonStatus值。 DataSetdsStatus=SqlHelper.GetData("selectPersonStatusfromSys_UserwherePersonCode='"+userName+"'"); //取出dsStatus(小数据库)中([0])第一张表的第一行中名为PersonStatus的列的值 stringpersonStatus=dsStatus.Tables[0].Rows[0]["PersonStatus"].ToString(); if(personStatus!="正常") { ScriptHelper.ShowAlertScript("用户状态不正确:"+personStatus); return; } //注意密码的加密,空字符加密后便不是空字符了。数据库中的密码是加密后的字符,实际比较中需要用实际输入字符经加密得到的字符与数据库中的比较 //判断密码法一 //stringsql="select*fromSys_UserwherePersonCode='{0}'andPassword='{1}'"; //DataSetdsUser=SqlHelper.GetData(string.Format(sql,userName,passWord.Ext_EncryptString())); //if(dsUser.Tables[0].Rows.Count!=1) //{ //ScriptHelper.ShowAlertScript("密码不正确!"); //return; //} //判断密码法二 stringsql="select*fromSys_UserwherePersonCode='{0}'"; DataSetdsUser=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(); //如果登录成功跳转到首页 Response.Redirect("index.aspx"); } } }
以上所述是小编给大家介绍的利用DataSet部分功能实现网站登录,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!