简体   繁体   中英

MySqlConnection.Open hangs on indefinitely

I'm creating a desktop application that connects to a SQL database. I'm having trouble connecting to the database for some reason, the connection hangs on indefinitely without giving an error (does no timeout like it should). I'm using XAMPP for my database. I've Googled the problem extensively I have tried for example changing the connection string and turning off the firewall.

Here is my code:

string connectionString = "Server=localhost;Database=typewrite;Uid=root;Pwd=";

    public int signIn(string username, string password)
    {
        int signinNum = -1;
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            try
            {
                conn.Open(); // here is the problem
            }
            catch (MySqlException ex)
            {
                Debug.WriteLine(ex.ToString());
                return signinNum;
            }

            using (MySqlCommand cmd = conn.CreateCommand())
            {
                try
                {
                    cmd.CommandText = @"SELECT 'USER_id' WHERE USER_name = @name AND pass = @passw";
                    cmd.Parameters.AddWithValue("@name", username);
                    cmd.Parameters.AddWithValue("@passw", password);
                    MySqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        signinNum = reader.GetInt32(0);
                    }
                }
                catch (MySqlException ex)
                {
                    return signinNum;
                }
            }
        }
        return signinNum;
    }

I have tried diffrent variations of this without prevail. I know I should use mutli thredding so it won't stop my application how ever I would like to get a connection to the database before I go any further.

And on top of that I have done something like this before and it connected with out a problem.

我发现了为什么它无法连接,我不得不为xampp数据库创建一个密码,并将其放在我的连接字符串中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM