当前位置: 移动技术网 > IT编程>数据库>其他数据库 > 利用DataSet部分功能实现网站登录

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

2017年12月01日  | 移动技术网IT编程  | 我要评论
首先,我之前必须完成过注册,并把个人信息存入数据库中。 其次,这部分的个别对象是存于某些文档中的,需要引用命名空间。 using system; using

首先,我之前必须完成过注册,并把个人信息存入数据库中。

其次,这部分的个别对象是存于某些文档中的,需要引用命名空间。

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部分功能实现网站登录 ,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网