2014-12-05 126 views
0

我需要帮助连接到我的Web主机上的MySQL服务器。下面是我的登录按钮的代码,但它无法登录我进去。连接到MySQL数据库C#

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) 
+0

具体*如何*失败? – David 2014-12-05 18:10:34

+0

“无法连接到任何指定的MySQL主机。” @Steve – 2014-12-05 18:11:37

+0

而你的虚拟主机是你的本地主机? – Steve 2014-12-05 18:12:29

回答

0

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

0

尝试通过Server=127.0.0.1改变Server=localhost如果它的工作验证您的hosts文件,并设置一个正确重定向...否则与MySQL验证提示该用户如果存在,则存在验证,验证它是否正确写入连接字符串