为什么我的代码没有读取if语句中的下一个条件 [英] Why my code is not reading the next condition in if statement

查看:78
本文介绍了为什么我的代码没有读取if语句中的下一个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的代码无法读取if语句中的下一个条件?

Why isn't my code reading the next condition in the if statement?

MySqlCommand cmd = new MySqlCommand("Select * from admin where Username='" + txtuser.Text + "'  and Password='" + txtpass.Text + "' ", con);

dr = cmd.ExecuteReader();
if (dr.Read())
{
    if (txtuser.Text == dr["Username"].ToString() && txtpass.Text == dr["Password"].ToString())
    {

        this.Hide();
        Form f1 = new Form1();
        f1.Show();
    }
    else if (txtuser.Text != dr["Username"].ToString() && txtpass.Text == dr["Password"].ToString())
    {
        MessageBox.Show("Incorrect Username!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        clear();
    }
    else if (txtuser.Text == dr["Username"].ToString() && txtpass.Text != dr["Password"].ToString())
    {
        MessageBox.Show("Incorrect Password!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        clear();
    }
    else if (txtuser.Text == "" & txtpass.Text == "")
    {
        MessageBox.Show("Please Enter Username and Password!", "Message", MessageBoxButtons.OK,
            MessageBoxIcon.Error);

    }
    else if (txtuser.Text == "" && txtpass.Text != "")
    {
        MessageBox.Show("Please Enter Username!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        clear();
    }
    else if (txtuser.Text != "" && txtpass.Text == "")
    {
        MessageBox.Show("Please Enter Password!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        clear();
    }
    else if (txtuser.Text != dr["Username"].ToString() && txtpass.Text != dr["Password"].ToString())
    {
        MessageBox.Show("Incorrect Username and Password!", "Message", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
        clear();
    }
}

推荐答案

(如果是&& C#中的条件,如果第一个条件为假,则下一个条件将不被验证

in case of && condition in C# if 1st become false then next condition is not going to be validated

这篇关于为什么我的代码没有读取if语句中的下一个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆