SqlConnection myconn = new SqlConnection("Data Source=localhost;Initial Catalog=zy;Integrated Security=True");
        myconn.Open();
        SqlCommand cmd = myconn.CreateCommand();
        cmd.CommandText = "SELECT [UserName], [PassWord] FROM [UserTable]";
        SqlDataReader reader = cmd.ExecuteReader();
        string name = TextBox1.Text;
        string pwd = TextBox2.Text;
        while (reader.Read())
        {            if (name == reader["UserName"].ToString())
            {
                if (pwd == reader["PassWord"].ToString())
                {
                    Response.Redirect("Default3.aspx");
                    return;
                }
                else
                {
                    Response.Redirect("error2.aspx");
                    return;
                }
            }
            else
                Response.Redirect("error3.aspx");
            return;
        }
        reader.Close();
        myconn.Close();
    }

解决方案 »

  1.   

    还有我的数据库中数据是这样的 UserName           PassWord
                              [email protected]      zhangyu当我再添加一条数据的时候,用这条数据登陆时就跳转到error3页面,如果有这条数据不是应该跳转吧Default3页面吗?
      

  2.   

    可能是while   (reader.Read())这里查的是空的话,就直接 执行:
    reader.Close(); 
    myconn.Close(); 
    就不会跳转了;
    另外,你的return在这里没有用,可以去掉。
      

  3.   

    可能是while       (reader.Read())这里查的是空的话,就直接   执行
    这里不是查询数据库吗?
      

  4.   

    你的SQl语句怎么不加Where条件啊
      

  5.   

    当我再添加一条数据的时候,用这条数据登陆时就跳转到error3页面,如果有这条数据不是应该跳转吧Default3页面吗?
    ======================================================
    比较的时候加.Trim()方法,防止有空格造成比较值不相等;
      

  6.   

    那我应该怎么改一下?cherry.8楼的大哥
      

  7.   

    你在数据库里用查询器执行
    SELECT   [UserName],   [PassWord]   FROM   [UserTable]
    看看是不是你想要的,你这语句有问题
      

  8.   

    你在数据库里用查询器执行 
    SELECT       [UserName],       [PassWord]       FROM       [UserTable] 
    看看是不是你想要的,你这语句有问题
    执行后显示[email protected] PassWord=zhangyu
      

  9.   

    在数据库里让UserName唯一,再写上SQL "SELECT UserName, PassWord FROM UserTable Where UserName='"+name+"'"
      

  10.   

    SqlConnection   myconn   =   new   SqlConnection("Data   Source=localhost;Initial   Catalog=zy;Integrated   Security=True"); 
                    myconn.Open(); 
                    string   name   =   TextBox1.Text.Trim(); 
                    string   pwd   =   TextBox2.Text.Trim(); 
                    SqlCommand   cmd   =   myconn.CreateCommand(); 
                    cmd.CommandText   =   "SELECT   [UserName],   [PassWord]   FROM   [UserTable] WHERE UserName='"+name+; 
                    SqlDataReader   reader   =   cmd.ExecuteReader(); 
                    while   (reader.Read()) 
                    {                         if   (name   ==   reader["UserName"].ToString()) 
                            { 
                                    if   (pwd   ==   reader["PassWord"].ToString()) 
                                            Response.Redirect("Default3.aspx"); 
                                    else 
                                            Response.Redirect("error2.aspx"); 
                            } 
                            else 
                                    Response.Redirect("error3.aspx"); 
                    } 
                    reader.Close(); 
                    myconn.Close(); 
            }我照你的那个意思这样小小修改了一下
      

  11.   

    那PassWord字段呢 也加在后面吗?PassWord='"+pwd+"'
      

  12.   

    首先~~SQL语句~~改成:
    SELECT   [UserName],   [PassWord]   FROM   [UserTable] where [UserName] = '" + name + "'判定语句改成
    while   (reader.Read()) 
    {     if   (name   ==   reader["UserName"].ToString() && pwd   ==   reader["PassWord"].ToString()) 
        { 
             Response.Redirect("Default3.aspx"); 
             return;  
        } 
        else 
        {
           Response.Redirect("error3.aspx"); 
           return; 
        } 这样试下`
      

  13.   

    http://edu.itbulo.com/200606/99519.htm
    登陆教程,好好学习一下
    相关文章里有后台登陆代码
      

  14.   

    提个小小意见,最好把这个写成一个函数根据返回者来判读跳转,你现在这么做,你想过没有,如果redirect后数据库关闭了吗
      

  15.   

    SqlConnection   myconn   =   new   SqlConnection("Data   Source=localhost;Initial   Catalog=zy;Integrated   Security=True"); 
                    myconn.Open(); 
                    SqlCommand   cmd   =   myconn.CreateCommand(); 
                    cmd.CommandText   =   "SELECT    [PassWord]   FROM   [UserTable] where [UserName]='"+name+"'"; 
                    object o =   cmd.ExecuteScalar(); 
                    string   name   =   TextBox1.Text; 
                    string   pwd   =   TextBox2.Text; 
                    if (o!=null)
                    {                         if   (pwd   ==  o.ToString()) 
                            { 
                                            myconn.Close(); 
                                            Response.Redirect("Default3.aspx"); 
                                            return; 
                            } 
                            else 
                            {
                                            myconn.Close();
                                            Response.write("<script>alert('密码错误')</script>");
                                            return; 
                            }
                    } 
                    else
                    {
                                 myconn.Close(); 
                                 Response.write("<script>alert('用户不存在')</script>");
                                 return;
                    }        
    }