C#。当我尝试检查用户是否存在于我的 SQL 数据库中时,我的 Windows Forms 应用程序崩溃了

C#. My Windows Forms App crashes when I try to check whether or not the user exists in my SQL database

我有一个简单的程序,您可以在文本框中输入登录名和密码,它会检查我的本地 SQL 数据库中是否存在这样的用户名和密码。除 DataReader 外,一切正常。当我输入登录名和密码然后按下按钮时,我的应用程序只是关闭而不是显示错误(如果密码不正确)、打开新表单(如果成功)或只是告诉我我的程序出了什么问题。当我从“if”语句的条件字段中删除 rd.HasRows 时,我的程序运行良好,所以我认为问题出在这里。

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-MR9T8DF\YEET;Initial Catalog=Students;Integrated Security=True");

            Form2 studentForm = new Form2();
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Students where Login = '" + LoginTextBox.Text + "' and Password = '" + PasswordTextBox.Text + "'", con);
                SqlDataReader rd = cmd.ExecuteReader();

                if (rd.HasRows)
                {
                    studentForm.Show();
                }
                else
                {
                    ErrorLabel.Visible = true;
                }

            Hide();
        }

您总是在登录评估后隐藏您的表单。删除此行:

Hide();