我正在尝试学习.WPF,但是我已经遇到了障碍。
我正在尝试通过文本框获取2个用户输入字符串,并将两个字符串都与值进行比较
从mySQL数据库检索。

下面的findUserFromDB函数:(应该用来检索和比较用户值)

    public static void findUserFromDB(string user, string pass)
    {
        List<User> users = new List<User> { };
        MySqlDataReader reader = null;
        bool userFound = false;
        MainWindow main = new MainWindow();

        try
        {
            myConn.Open();
            MySqlCommand cmd = new MySqlCommand();
            //Sets command text and connection database
            cmd.CommandText = string.Format("Select * From users");
            cmd.Connection = myConn;
            reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                User forUser = new User
                {
                    sName = reader.GetString("Forename").Trim(),
                    sPass = reader.GetString("Password").Trim(),
                };
                users.Add(forUser);

            }
            if (userFound == false)
            {
                MessageBoxResult msg = MessageBox.Show(main, "Username or Password" +
                    " not recognised.", "Login failed", MessageBoxButton.OK);
            }
            for (int i = 0; i < user.Count(); i++)
            {
                if (users[i].sName == user && users[i].sPass == pass)
                {
                    Menu m = new Menu();
                    string message = "Welcome back " + user + ".";
                    MessageBoxResult msg = MessageBox.Show(main, message,
                        "Login successful", MessageBoxButton.OK);
                    m.Show();
                    main.Close();
                    break;
                }
            }
        }
        catch (MySqlException e)
        {

        }
        finally
        {
            if (reader != null)
            {
                reader.Close(); //Close the reader
            }
            if (myConn != null)
            {
                myConn.Close(); //ensure you close the connection
            }
        }


现在查看它,我意识到在比较值之前,我应该先检索数据,然后关闭数据库...

我的问题:
我究竟做错了什么?
我按下按钮登录后,什么也没有发生。

最佳答案

我知道了

引用了错误的数据库^^,
天哪,我觉得很蠢...

关于c# - 比较来自MySQL的数据值(WPF-C#),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21909350/

10-09 00:16
查看更多