2014-12-05 124 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驗證提示該用戶如果存在,則存在驗證,驗證它是否正確寫入連接字符串