利用DataSet部分功能实现网站登录

时间:2019-03-30
本文章向大家介绍利用DataSet部分功能实现网站登录,主要包括利用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;//后面用到ScriptHelper对象(ScriptHelper.cs是自己编写的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)
 {
//用户表 Sys_User 列PersonStatus 为 “正常” 才可登录 不然提示账户状态为PersonStatus内的内容
//列PersonCode为用户名 PassWord为密码
//数据库中PassWord保存的为加密后的 字符串.Ext_DecryptString();为解密 Ext_EncryptString();为加密
string userName = txtUserName.Text.Trim();//.Trim()是去掉字符串前后的空字符
string passWord = 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表示有。
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(小数据库)中([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[0]["PersonName"].ToString();
 Session["UserID"] = dsUser.Tables[0].Rows[0]["ItemID"].ToString();
 //如果登录成功 跳转到首页
Response.Redirect("index.aspx");
 }
 }
}

以上所述是小编给大家介绍的利用DataSet部分功能实现网站登录 ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!