con = new SqlConnection(@"Data Source=owen\SQL2008;Initial Catalog=mydb;Persist Security Info=True;User ID=sa;password=sa");
            com = new SqlCommand("select * from StudentClass", con);
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                /*
                       就是就里怎么于登陆窗体的textbox1(用户名)和textbox2(密码)比较是否相等??
                          */
            }
            dr.Close();
            con.Close();怎么比较textbox1(用户名)和textbox2(密码)比较是否相等??

解决方案 »

  1.   

    只是为了验证用户登陆,不应该读取用户表的所有数据。
    应该是把用户名和密码作为sql参数,查询到有符合的记录,则验证成功,否则失败。reader.GetString(0) 获取第一列的字符串值。
      

  2.   

    while (dr.Read()) 

        /* 
         就是就里怎么于登陆窗体的textbox1(用户名)和textbox2(密码)比较是否相等?? 
        */ 
        if(reader["UserName"].ToString()==textbox1.Text & reader["PassWord"].ToString()==TextBox2.Text)
        {
          //通过
        }
        else
        {
          //非法
        }

    其中UserName,PassWord是楼主表中字段名!!
      

  3.   

    string sql = @"select studentid,classid from class where studentid='" + textBox1.Text.Trim() + "' and classid='" + textBox2.Text.Trim() + "'";
                SqlConnection con = new SqlConnection(@"Data Source=owen\SQL2008;Initial Catalog=mydb;Persist Security Info=True;User ID=sa;password=sa");
                SqlCommand com = new SqlCommand(sql, con);
                try
                {
                    con.Open();
                    int i = (int)com.ExecuteScalar();
                    con.Close();
                    if (i != 0)
                    {
                        MessageBox.Show("登陆成功");
                    }
                    else
                    {
                        MessageBox.Show("登陆不成功");
                    }
                }
                catch (ExecutionEngineException )
                {
                    
                    throw;
                }当输入已有用户密码时就没报错.当输入没有的用户和密码是就报错了.
    说System.NullReferenceExecption 和{"未将对象引用设置到对象的实例。"}
      

  4.   

    有病啊。
    这样做吧 com = new SqlCommand("select count(*) from StudentClass where username="+text1.text+" and userpwd="+text2.text, con); 
      

  5.   

    谢谢!想问问为什么SQL语句要计算所有行呀.这样就连textbox是空也不会帮错.!