我无法验证用户的登录,因为如果我输入错误的值,我的数据读取器不会被执行吗?

发布于 2024-10-09 22:41:55 字数 1659 浏览 0 评论 0原文

//代码

private void glassButton1_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "" || textBox1.Text == "" || comboBox1.SelectedIndex == 0)
    {
        Message m = new Message();
        m.ShowDialog();
    }

    else
    {
        try
        {
            con.ConnectionString = "Data source=BLACK-PEARL;Initial Catalog=LIFELINE ;User id =sa; password=143";
            con.Open();
            SqlCommand cmd = new SqlCommand("Select LoginID,Password,Department from Login where LoginID=@loginID and Password=@Password and Department=@Department", con);
            cmd.Parameters.Add(new SqlParameter("@loginID", textBox1.Text));
            cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));
            cmd.Parameters.Add(new SqlParameter("@Department", comboBox1.Text));
            SqlDataReader dr = cmd.ExecuteReader();
           if (dr.Read())
            {

                string Strname = dr[0].ToString();
               string StrPass = dr[1].ToString();
               string StrDept = dr[2].ToString();
               if(dr[2].ToString().Equals(comboBox1.Text)&&dr[0].ToString().Equals(textBox1.Text)&&dr[1].ToString().Equals(textBox2.Text))
                {
                    MessageBox.Show("Welcome");
                }
                else
                {
                    MessageBox.Show("Please Enter correct details");
                }
            }
           dr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception" + ex);
        }
        finally
        {
            con.Close();
        }
    }
}

//code

private void glassButton1_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "" || textBox1.Text == "" || comboBox1.SelectedIndex == 0)
    {
        Message m = new Message();
        m.ShowDialog();
    }

    else
    {
        try
        {
            con.ConnectionString = "Data source=BLACK-PEARL;Initial Catalog=LIFELINE ;User id =sa; password=143";
            con.Open();
            SqlCommand cmd = new SqlCommand("Select LoginID,Password,Department from Login where LoginID=@loginID and Password=@Password and Department=@Department", con);
            cmd.Parameters.Add(new SqlParameter("@loginID", textBox1.Text));
            cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));
            cmd.Parameters.Add(new SqlParameter("@Department", comboBox1.Text));
            SqlDataReader dr = cmd.ExecuteReader();
           if (dr.Read())
            {

                string Strname = dr[0].ToString();
               string StrPass = dr[1].ToString();
               string StrDept = dr[2].ToString();
               if(dr[2].ToString().Equals(comboBox1.Text)&&dr[0].ToString().Equals(textBox1.Text)&&dr[1].ToString().Equals(textBox2.Text))
                {
                    MessageBox.Show("Welcome");
                }
                else
                {
                    MessageBox.Show("Please Enter correct details");
                }
            }
           dr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception" + ex);
        }
        finally
        {
            con.Close();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-10-16 22:41:55

当没有匹配的用户时,它将返回零行。
您应该检查阅读器是否有任何行,如果没有,则意味着登录详细信息错误。只需在该 if 语句中包含另一个 else 即可。

if (dr.Read())
{
.........
}
else
{
   MessageBox.Show("Please Enter correct details");
}

When there is no matching user it will return zero rows.
You should check if it reader has any rows if not then it means that login details are wrong. Just include another else with this if statement.

if (dr.Read())
{
.........
}
else
{
   MessageBox.Show("Please Enter correct details");
}
谢绝鈎搭 2024-10-16 22:41:55

您应该在 SQL 中将 && 替换为 AND

SELECT LoginID,Password,Department
FROM Login
WHERE LoginID=@loginID
AND Password=@Password 
AND Department=@Department

You should replace && with AND in your SQL:

SELECT LoginID,Password,Department
FROM Login
WHERE LoginID=@loginID
AND Password=@Password 
AND Department=@Department
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文