当前位置:编程学习 > C#/ASP.NET >>

怎么解决这个问题

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections;

public partial class login : System.Web.UI.Page
{
 
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //连接数据库
        SqlConnection sqlconstr = new SqlConnection();
        sqlconstr.ConnectionString = "server=localhost;database=employee;uid=sa;pwd='111111'";
        sqlconstr.Open();

        //Response.Write(sqlconstr.State.ToString());
        
        //获取客户端的信息
        string strUsername = this.Username.Text.Trim();
        string strPassword = this.Password.Text.Trim();

        //创建SQL语句
        string mysql = "select * from empInfo where username='" + strUsername+ "' and password='" + strPassword + "'";
        string mysql1 = "select * from empInfo where username='" + strUsername + "'";
        string mysql2 = "select * from empInfo where password='" + strPassword + "'";
        SqlCommand cmd = new SqlCommand(mysql, sqlconstr);
        SqlCommand cmd1 = new SqlCommand(mysql1,sqlconstr);
        SqlCommand cmd2 = new SqlCommand(mysql2, sqlconstr);
        SqlDataReader dr = cmd.ExecuteReader();
        SqlDataReader dr1 = cmd1.ExecuteReader();
        SqlDataReader dr2 = cmd2.ExecuteReader();
        if (dr.Read())
        {
            //Response.Write("登录成功");
            //Response.Write("<script>alert('登录成功');location='emplist'</script>");
           // Response.Redirect("emplist.aspx");
            string power=dr["per"].ToString();
            if (this.userRadio.Checked == true && power == "2")
            {
                Response.Redirect("emplist.aspx");
            }
            else
            {
                if (this.admRadio.Checked == true && power == "1")
                {
                    Response.Redirect("admManage.aspx");
                }
                else
                {
                    this.logMessage.Visible = true;
                    this.logMessage.Text = "你的用户权限有误,请重新登录";
                }
            }
        }
        else
        {
           
           
        
            //Response.Write("用户名或密码不正确");
            //this.Username.Text = "";
            //判断用户名错误还是密码错误
            if(!dr1.Read())
            {
                this.logMessage.Visible = true;
                this.logMessage.Text = "用户名错误";
            }
            if(!dr2.Read())
            {
                this.logMessage.Visible = true;
                this.logMessage.Text = "密码错误!";
            }
        }
        dr.Close();
        dr1.Close();
        sqlconstr.Close();

    }
} --------------------编程问答-------------------- --------------------编程问答-------------------- Read只能读取一次 --------------------编程问答--------------------
引用 2 楼 liuchaolin 的回复:
Read只能读取一次
那怎么解决 --------------------编程问答-------------------- 用DataTable来存储

"select top 1 * From username=@username"
....
DataTable dt=...
if(dt==null || dt.Rows.Count<=0){
  用户不存在
  return;
}

if(dt.Rows[0]["password"].ToString()!=strPassword){
   密码不正确
   return;
}

验证成功! --------------------编程问答-------------------- --------------------编程问答-------------------- 四楼正解~~~ --------------------编程问答-------------------- 为毛要两个一模一样的datareader? --------------------编程问答-------------------- 因为这样就可以读两次了 --------------------编程问答-------------------- 用tataset不就OK了 --------------------编程问答-------------------- 放到dataset里面,可以随便操作了
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,