下面是我写的代码,调试时read()总是返回false,这是为什么?
protected void Butt1_Click(object sender, EventArgs e)
        {
            //去除文本框空格
            STUserNickName = TBox1.Text.Trim();
            STUserPwd = TBox2.Text.Trim();
            //获得数据库连接字符串
            string Myconnection = ConfigurationSettings.AppSettings["strconnection"];
            //创建数据库连接
            SqlConnection Myconn = new SqlConnection(Myconnection);
            //打开数据库连接
            Myconn.Open();
            //根据用户名和密码创建sql语句
            string STstrsql = "select * from UserTable where UserName = '" + TBox1 + "' and UserPassword = '" + TBox2 + "'";
            //创建SqlCommand对象,对sql语句进行操作
            SqlCommand STcmd = new SqlCommand(STstrsql, Myconn);
            //通过调用ExecuteReader()从数据库中检索行,创建SqlDataReader实例
            SqlDataReader Reader = STcmd.ExecuteReader();
            //判断是否有数据
            if (Reader.Read())
            {
                //将获得的UserName保存到Session中
                Session["UserName"] = Reader["UserName"];
                //将获得的STNoAdmin保存到Session中
                Session["NoAdmin"] = Reader["NoAdmin"];
                //判断管理权限
                if (Session["NoAdmin"].ToString() == "0")
                {
                    //如果是一般用户就定向到STUserLogin.aspx页面
                    Response.Redirect("DepUser\\DepUserLogin.aspx");
                }
                else if (Session["STNoAdmin"].ToString() == "1")
                {
                    //如果是管理员就定向到STManagerLogin.aspx页面
                    Response.Redirect("Manager\\ManagerLogin.aspx");
                }
            }
            else
            {
                //用户名密码不正确就返回到Index.aspx页面
                Response.Write("<script language='javascript'>alert('登陆失败!');</script>");
                // Response.Redirect("Index.aspx");
            }
        }

解决方案 »

  1.   

    当代吗执行到if (Reader.Read()) 时返回FALSE所以跳过直接执行了ESlE
      

  2.   

     "select * from UserTable where UserName = '" + TBox1.Text.Trim(); + "' and UserPassword = '" + TBox2.Text.Trim(); + "'"; 
    你查询的怎么成控件名称了呢?
      

  3.   

    string STstrsql = "select * from UserTable where [UserName] = '" + TBox1.Text.Trim() + "' and UserPassword = '" + TBox2.Text.Trim() + "'"; 好像你的sql语句不正确
      

  4.   

    有数据查出来吗?
      带sql语句复制到查询管理器里试下看看有数据没