注册 登录
编程论坛 ASP.NET技术论坛

为什么老是显示登录失败?

fylnlx 发布于 2009-09-01 22:39, 836 次点击
protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select [name],password from te", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds,"te");
            SqlDataReader dr = cmd.ExecuteReader();
            string name = Convert.ToString(dr);
            dr.Close();
            SqlDataReader dr1 = cmd.ExecuteReader();
            string password = Convert.ToString(dr1);
            Response.Write(name);
            if(txtName.Text.ToString()==name&&txtPassword.Text.ToString()==password)
            {
                Response.Write("登录成功");
            }
            else
            {
                Response.Write("登录失败");
            }
            dr1.Close();
            conn.Close();
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtName.Text = "";
            this.txtPassword.Text = "";
        }
    }
7 回复
#2
dhbest2009-09-02 15:23
  SqlCommand cmd = new SqlCommand("select [name],password from te", conn);
  SqlDataReader dr = cmd.ExecuteReader();
  string name = Convert.ToString(dr);
  dr.Close();
  SqlDataReader dr1 = cmd.ExecuteReader();
  string password = Convert.ToString(dr1);

这里得出来的name,password都是sql语句查出来的[name],password 的值 ,然后又跟txtbox的值比较肯定是不相同的!
     你可以在查询语句后加个条件  “select [name],password from te where [name] = '"+txtName.Text.ToString()+"' and password = '"+txtPassword.Text.ToString()+"'”   如果有结果则成功否则失败!
#3
xiaobin90152009-09-02 17:37
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from te", conn);
            SQlDataReader dr = cmd.ExecuteReader();
            string name;
            string password;
            while(dr.Read())
            {
             name=(string)dr["用户名"];
             password=(string)dr["密码"];
            if(txtName.Text.ToString()==name&&txtPassword.Text.ToString()==password)
            {
                Response.Write("登录成功");
            }
            else
            {
                Response.Write("登录失败");
            }
            }
#4
visolleon2009-09-05 22:35
调试看看,不要老看你自己输出的内容! 看看有没有Exception
#5
bozl2009-09-07 14:20
sql语句
SqlCommand cmd = new SqlCommand("select count(*) from te where name="+txtName.Text.ToString()+" and "+"password="+txtPassword.Text.ToString(), conn);
#6
daygujun2009-09-07 22:08
我的建议先是:你使用存储过程.
另外把包错情况说出来.
#7
aganarRMJ2009-09-10 16:42
语句显得很乱,你可以把用户和密码的值当做参数传递到SQL内,作为查询的一种条件,让返回的数据集为顶多只含有一条记录,在来一个试探该数据集是否有内容判断登录成功与否就大功告成了的!
#8
xudongcsharp2009-09-11 09:44
楼上正解
1