连接MySQL数据库C#

时间:2014-12-05 18:06:37

标签: c# mysql database-connection

我需要帮助连接到我的网络主机上的MySQL服务器。这是我的登录按钮的代码,但它无法登录。

private void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string strConnect = "Server=localhost;Database=cpr_users;Uid=MyUsername;password=*******;";
            MySqlConnection myConn = new MySqlConnection(strConnect);
            MySqlCommand selectCmd = new MySqlCommand("select * from cpr_users.cpr_user_info where username='" + txtUsername.Text + "' and password='" + txtPassword.Text + "' ;", myConn);
            MySqlDataReader myReader;
            myConn.Open();

            int count = 0;
            myReader = selectCmd.ExecuteReader();
            while (myReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                this.Close();
                MedicalForm();
            }
            else if (count > 1)
            {
                MessageBox.Show("Theres 2 Users with that username Please contact Your_Name_Here ...Access Denied");
            }
            else
                MessageBox.Show("Username or Password is Not correct .. Please try Again!");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我不确定我是否使用了错误的连接信息

当我使用我的虚拟主机专用IP并登录时,我收到以下错误

Authentication to host '**IP ADDRESS**' for user 'MyUsername' using method 'mysql_native_password' failed with message: Access denied for user 'MyUsername'@'myipaddress' (using password: YES)

2 个答案:

答案 0 :(得分:0)

尝试更改Server=localhost Server=127.0.0.1如果有效,请验证您的hosts文件并设置正确的重定向...否则请使用Mysql提示验证用户& pass存在(如果存在)验证它是否正确写入连接字符串

答案 1 :(得分:0)

我错过了连接字符串中的端口号。