簡體   English   中英

連接到MySQL數據庫C#

[英]Connecting to MySQL Database C#

我需要幫助連接到Web主機上的MySQL Server。 這是我的“登錄”按鈕的代碼,但無法登錄。

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);
        }
    }

我不確定我是否使用了錯誤的信息進行連接

當我使用Webhost的專用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)

嘗試通過Server=127.0.0.1通過Server=127.0.0.1更改Server=localhost驗證hosts文件並設置正確的重定向...否則使用Mysql提示符驗證用戶名和密碼是否存在(如果存在)驗證其是否正確寫入了您的連接字符串

我在連接字符串中缺少端口號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM