根据书上的案例自己编写了一个餐厅管理系统可是在运行时登陆时输入账户密码 却提示输入用户名密码错误代码如下
  private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSubmit_Click(sender, e);
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
             if (txtName.Text == "")          
            {
                MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (txtPwd.Text == "")
                {
                    MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                  //SqlConnection conn = BaseClass.DBConn.CyCon();   
                    string str = "Data Source=127.0.0.1;Initial Catalog=cantingguanlixitong;Integrated Security=True";
                    SqlConnection conn = new SqlConnection(str);
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "' and themima='" + txtPwd.Text + "'", conn);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    sdr.Read();
                    if (sdr.HasRows)
                    {
                        cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "'", conn);
                         sdr = cmd.ExecuteReader();
                        sdr.Read();
                        string UserPower = sdr["quanxian"].ToString().Trim();
                        conn.Close();
                        Form2  main = new Form2  ();
                        main.Show();

解决方案 »

  1.   

    代码中没有提示“输入用户名密码错误”的地方,是不是代码贴的不全啊。在btnSubmit_Click事件中加个断点debug一下,看看那个地方跑错了~
      

  2.   

    click事件中加断点dubug一下,代码没贴全吧,没看到有提示错误的代码...
      

  3.   


     if (sdr.HasRows)
      {
     //此处为何又查询一次数据库?
      cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "'", conn);
      sdr = cmd.ExecuteReader();
      sdr.Read();
      string UserPower = sdr["quanxian"].ToString().Trim();
     
      conn.Close();
      Form2 main = new Form2 ();
      main.Show();
     string str = "Data Source=127.0.0.1;Initial Catalog=cantingguanlixitong;Integrated Security=True";
    ========
     string str = "Data Source=.\sqlexpress;Initial Catalog=cantingguanlixitong;Integrated Security=True";你的数据库是否是express版本
      

  4.   

    代码没贴全吧,没看到有提示错误的代码..click事件中加断点,debug一下