C#登录代码无法正常工作

时间:2017-02-26 19:54:33

标签: c# mysql login

我正在尝试使用跨表参数化SQL创建登录代码。代码不起作用,我不知道为什么。

C#代码:

    using (MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;database=project;username=root;password=***;"))
{
    MySqlCommand cmd = new MySqlCommand(@"SELECT  * FROM student, teacher 
     WHERE student.username = @userName AND student.password = @passWord 
     AND teacher.username = @teacherUser AND teacher.password = @teacherPass;");

    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    cmd.Parameters.AddWithValue("@userName", textboxUsername.Text);
    cmd.Parameters.AddWithValue("@passWord", texBoxPassword.Text);
    cmd.Parameters.AddWithValue("@teacherUser", textBoxTeacherUsername.Text);
    cmd.Parameters.AddWithValue("@teacherPass", textBoxTeacherPassword.Text);
    connection.Open();    
    MySqlDataReader DBReader;
    DBReader = cmd.ExecuteReader();
    cmd.Connection = connection;
    int check = 0;
    while (DBReader.Read())
    {
        check = check + 1;
    }
    if (check == 1)
    {
        MessageBox.Show(" Login Successfull");
        this.Hide();
        Test ss = new Test();
        ss.Show();
    }
    else
    {
         MessageBox.Show("Username or Password Incorrect.  Try Again please");
    }
    connection.Close();

}

我希望它能够使用正确的用户名和密码授予学生或教师(不同用户)的访问权限。但是我一直得到相同的输出:

  

用户名或密码不正确。请再试一次

     MySqlCommand cmd = new MySqlCommand("SELECT * FROM student, teacher WHERE (student.username = @userName AND student.password = @passWord) OR (teacher.username = @teacherUser AND teacher.password = @teacherPass);");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@userName", textboxUsername.Text);
            cmd.Parameters.AddWithValue("@passWord", texBoxPassword.Text);
            cmd.Parameters.AddWithValue("@teacherUser", textboxUsername.Text);
            cmd.Parameters.AddWithValue("@teacherPass", texBoxPassword.Text);
            connection.Open();
            MySqlDataReader DBReader;
            cmd.Connection = connection;
            DBReader = cmd.ExecuteReader();
            int check = 0;
            while (DBReader.Read())
            {
                var teacherLogin = DBReader.GetString("teacher.username");
                var teacherPass = DBReader.GetString("teacher.password");
                var studentLogin = DBReader.GetString("student.username");
                var studentPass = DBReader.GetString("student.password");

                if (teacherLogin == textboxUsername.Text && teacherPass == texBoxPassword.Text)
                {
                    MessageBox.Show("Login Successfull!");

                    this.Hide();
                    Registration ss = new Registration();
                    ss.Show();
                }
                else if (studentLogin == texBoxPassword.Text && studentPass == texBoxPassword.Text)
                {
                    MessageBox.Show(" Login Successfull");

                    this.Hide();
                    Test ss = new Test();
                    ss.Show();
                }
                else
                {
                    MessageBox.Show("Username or Password Incorrect.  Try Again please");
                }

            }
  

MySql.Data.dll中发生未处理的“System.IndexOutOfRangeException”类型异常

     

其他信息:在结果中找不到指定的列:teacher.username

1 个答案:

答案 0 :(得分:1)

也许你必须在sql查询中使用OR条件?

SELECT * FROM student, teacher WHERE (student.username = @userName AND student.password = @passWord) OR (teacher.username = @teacherUser AND teacher.password = @teacherPass)