【问题标题】:C#. My Windows Forms App crashes when I try to check whether or not the user exists in my SQL databaseC#。当我尝试检查用户是否存在于我的 SQL 数据库中时,我的 Windows 窗体应用程序崩溃
【发布时间】:2021-03-05 12:20:37
【问题描述】:

我有一个简单的程序,你在文本框中输入登录名和密码,它应该检查我的本地 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();
        }

【问题讨论】:

    标签: c# sql winforms crash


    【解决方案1】:

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

    Hide();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      相关资源
      最近更新 更多