如何将方法中的数据提取到返回调用的外部方法?

时间:2016-06-30 17:37:37

标签: c# mysql wpf

我知道这是纯文本并且不安全,但我正在尝试学习如何使用WPF c#.net框架创建登录表单。这是一个继承问题,但我不确定如何将数据从数据库中获取到底部的其余方法(我已经注释掉了,以便向您展示我想要做的事情)。

    private bool validateUser(string username, string password)
    {
        if (UserName != "" & Password != "")
        {
            server = "localhost";
            database = "test";
            uid = "username";
            password = "password";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            MySqlConnection con = new MySqlConnection(connectionString);
            // MySqlConnection con = new MySqlConnection(@"Data Source=USER;Initial Catalog=admin;Integrated Security=True"); 
            MySqlDataAdapter sda = new MySqlDataAdapter("SELECT COUNT(*) FROM table1 WHERE username='" + UserName + "' AND password='" + Password + "'", con);
            /* in above line the program is selecting the whole data from table and the matching it with the user name and password provided by user. */
            DataTable dt = new DataTable(); 
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                /* I have made a new page called home page. If the user is successfully authenticated then the form will be moved to the next form */
                User validatedUser = userList.FirstOrDefault(user => user.UserName.Equals(username) && user.Password.Equals(password));//I need this to be returned to the main method so I can make this work validateUser
                return validatedUser != null;//Here is the return that is being called for the main method validateUser
                MessageBox.Show("connected");
            }
            else
                MessageBox.Show("Invalid username or password");
            }

            // we need to try and get the database to this method
        //User validatedUser = userList.FirstOrDefault( user => user.UserName.Equals( username ) && user.Password.Equals( password ));
        //return validatedUser != null; //This is where it works, but doesn't pull from the processes above because of inheritance

    }

1 个答案:

答案 0 :(得分:0)

这听起来像是你的问题:

private bool validateUser(string username, string password)
{
    if (UserName != "" & Password != "")
    {
        server = "localhost";
        database = "test";
        uid = "username";
        password = "password";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        MySqlConnection con = new MySqlConnection(connectionString);
        // MySqlConnection con = new MySqlConnection(@"Data Source=USER;Initial Catalog=admin;Integrated Security=True"); 
        MySqlDataAdapter sda = new MySqlDataAdapter("SELECT COUNT(*) FROM table1 WHERE username='" + UserName + "' AND password='" + Password + "'", con);
        /* in above line the program is selecting the whole data from table and the matching it with the user name and password provided by user. */
        DataTable dt = new DataTable(); 
        sda.Fill(dt);
        if (dt.Rows[0][0].ToString() == "1")
        {

            MessageBox.Show("connected");
        }
        else
            MessageBox.Show("Invalid username or password");
        }

        // we need to try and get the database to this method
    User validatedUser = userList.FirstOrDefault( user => user.UserName.Equals( UserName ) && user.Password.Equals( Password ));
    return validatedUser != null; //This is where it works, but doesn't pull from the processes above because of inheritance

}