private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "" || this.textBox2.Text == "")
            {
                MessageBox.Show("用户名或密码不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox1.Focus();
            }
            else
            {
                string strcon = "Server=(local);Integrated Security=true;database=stu_info_manage";
                try
                {
                    SqlConnection conn = new SqlConnection(strcon);
                    conn.Open();
                    string str = "select * from user_info where user_name='" + this.textBox1.Text.Trim() + "'";
                    SqlCommand cmd = new SqlCommand(str, conn);
                    SqlDataReader read = cmd.ExecuteReader();
                    while (read.Read())
                    {
                        if (this.textBox2.Text.Trim() == read.GetString(1))
                        {
                            this.Hide();
                            Form3 newform3 = new Form3();
                            newform3.Show();
                        }
                        else
                        {
                            MessageBox.Show("密码错误!");
                        }
                    }
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
这是我做的登陆界面出现的问题。按照数据库里添加的user_name和user_psw输入。总显示密码错误。

解决方案 »

  1.   

    我建议你 sql 语句这样
    string str = "select count(*) from user_info where user_name='" + this.textBox1.Text.Trim() + "' and 密码列='" + this.textBox2.Text.Trim() + "'";
    然后查询的地方
    SqlCommand cmd = new SqlCommand(str, conn);
    int count = Convert.ToInt32(cmd.ExecuteScalar());
    conn.Dispose();
    if (count > 0)
    {
        this.Hide();
        Form3 newform3 = new Form3();
        newform3.Show();
    }
    else
    {
        MessageBox.Show("密码错误!");
    }
    如果再密码错误,就断点到 SqlCommand cmd = new SqlCommand(str, conn); 这行,取出 str 的值,将条件值和数据库表中的值比较下,再放到查询器里去执行下看看结果
      

  2.   

    read.GetString(1) 你数据库用户信息表里第二列就是密码?
      

  3.   

    这个表就两列。一个user_name,一个user_psw